zoukankan      html  css  js  c++  java
  • Delphi中ADO之初识

    -----------开发环境Delphi7

    需要Uses ComOb, ADOInt;

    ----图片

     不要去纠结报错问题,哈哈哈

     --------------------------------

    ------------------

    SQL Server 2008 Express版的

    ----------用到一个表

    CREATE TABLE [dbo].[Test_01](
    [MYID] [varchar](20) NOT NULL,
    [MYName] [varchar](50) NULL
    )

     ----------

    insert into Test_01 values('A001','Test_A001'),('A002','Test_A002'),('A003','Test_A003')

    这里插入三笔数据

    -------------------------------

    注意这个的配置:

    const ConStr:string='Provider=SQLOLEDB.1;Password=sa123456;Persist Security Info=True;User ID=sa;Initial Catalog=Test;Data Source=192.168.1.50SQLEXPRESS';

    Provider=SQLOLEDB.1;

    Password=sa123456;//密码

    Persist Security Info=True;

    User ID=sa;//登入名

    Initial Catalog=Test;//数据库名称

    Data Source=192.168.1.50SQLEXPRESS//数据库来源,,一般填写IP就行,我这个是精简版的SQL Server 2008 所以用了192.168.1.50SQLEXPRESS

    如果觉得这个太坑,请拖一个ADOConnection控件配置好,再把ADOConnection.ConnectionString拷贝出来就行;

    -------------------------------------------------------------------------------------------------------------------------------------------------------

    ----Unit开始

      1 unit Unit1;
      2 
      3 interface
      4 
      5 uses
      6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      7   Dialogs, ComObj, StdCtrls, ADOInt;
      8 
      9 type
     10   TForm1 = class(TForm)
     11     Button1: TButton;
     12     Button2: TButton;
     13     Edit1: TEdit;
     14     Edit2: TEdit;
     15     Button3: TButton;
     16     Button4: TButton;
     17     Button5: TButton;
     18     Button6: TButton;
     19     Button7: TButton;
     20     procedure Button1Click(Sender: TObject);
     21     procedure Button2Click(Sender: TObject);
     22     procedure Button3Click(Sender: TObject);
     23     procedure Button4Click(Sender: TObject);
     24     procedure Button5Click(Sender: TObject);
     25     procedure Button6Click(Sender: TObject);
     26     procedure Button7Click(Sender: TObject);
     27   private
     28     procedure SetEditText;
     29     function CheckQueryActive:Boolean;
     30     { Private declarations }
     31   public
     32     { Public declarations }
     33   end;
     34 
     35 var
     36   Form1: TForm1;
     37 
     38 implementation
     39 
     40 var
     41   ADOCon,ADOQuery:OleVariant;
     42 const ConStr:string='Provider=SQLOLEDB.1;Password=sa123456;Persist Security Info=True;User ID=sa;Initial Catalog=Test;Data Source=192.168.1.50SQLEXPRESS';
     43 {$R *.dfm}
     44 
     45 procedure TForm1.Button1Click(Sender: TObject);
     46 begin
     47   //连接数据库
     48   if VarIsEmpty(ADOCon) then
     49     ADOCon:=CreateOleObject('ADODB.Connection');
     50   //if VarIsNull(ADOCon) then
     51    // ADOCon:=CreateOleObject('ADODB.Connection');
     52   ADOCon.Open(ConStr);
     53 end;
     54 
     55 procedure TForm1.Button2Click(Sender: TObject);
     56 begin
     57   ADOCon.Close;
     58 end;
     59 
     60 procedure TForm1.Button3Click(Sender: TObject);
     61 begin
     62   if VarIsEmpty(ADOCon) then
     63   begin
     64     ShowMessage('数据库没有连接!');
     65     Exit;
     66   end;
     67   {if VarIsNull(ADOCon) then
     68   begin
     69     ShowMessage('数据库没有连接!');
     70     Exit;
     71   end;}
     72   ADOQuery:=CreateOleObject('ADODB.Recordset');
     73   ADOQuery.ActiveConnection:=ADOCon;
     74   ADOQuery.Open('select * from Test_01',ADOCon,adOpenStatic,adLockOptimistic,adCmdText);
     75   SetEditText;
     76 end;
     77 
     78 procedure TForm1.SetEditText;
     79 begin
     80   Edit1.Text:=ADOQuery.Fields['MYID'].Value ;
     81   Edit2.Text:=ADOQuery.Fields['MYName'].Value ;
     82 end;
     83 
     84 procedure TForm1.Button4Click(Sender: TObject);
     85 begin
     86   if not CheckQueryActive then
     87     Exit;
     88   if not ADOQuery.Eof then  //这个最后一笔判断不准啊
     89   begin
     90     ADOQuery.MoveNext;
     91     SetEditText;
     92   end;
     93 end;
     94 
     95 procedure TForm1.Button5Click(Sender: TObject);
     96 begin
     97   if not CheckQueryActive then
     98     Exit;
     99   if not ADOQuery.Bof then //这个首笔判断不准啊
    100   begin
    101     ADOQuery.MovePrevious;
    102     SetEditText;
    103   end;
    104 end;
    105 
    106 function TForm1.CheckQueryActive: Boolean;
    107 begin
    108   Result:=False;
    109   if VarIsEmpty(ADOQuery) then
    110     Exit;
    111   //if VarIsNull(ADOQuery) then
    112   //  Exit;
    113   Result:=True;
    114 end;
    115 
    116 procedure TForm1.Button6Click(Sender: TObject);
    117 begin
    118   if not CheckQueryActive then
    119     Exit;
    120   if not ADOQuery.Bof then  //这个首笔判断不准啊
    121   begin
    122     ADOQuery.MoveFirst;
    123     SetEditText;
    124   end;
    125 end;
    126 
    127 procedure TForm1.Button7Click(Sender: TObject);
    128 begin
    129   if not CheckQueryActive then
    130     Exit;
    131   if not ADOQuery.Eof then  //这个最后一笔判断不准啊
    132   begin
    133     ADOQuery.MoveLast;
    134     SetEditText;
    135   end;
    136 end;
    137 
    138 end.
    139 //ADOQuery.RecordCount 记录数量

    -------Unit结束

    ----Form开始

     1 object Form1: TForm1
     2   Left = 885
     3   Top = 404
     4   BorderStyle = bsDialog
     5   Caption = 'Form1'
     6   ClientHeight = 257
     7   ClientWidth = 319
     8   Color = clBtnFace
     9   Font.Charset = DEFAULT_CHARSET
    10   Font.Color = clWindowText
    11   Font.Height = -11
    12   Font.Name = 'MS Sans Serif'
    13   Font.Style = []
    14   OldCreateOrder = False
    15   PixelsPerInch = 96
    16   TextHeight = 13
    17   object Button1: TButton
    18     Left = 0
    19     Top = 24
    20     Width = 137
    21     Height = 25
    22     Caption = 'Button1_连接数据库'
    23     TabOrder = 0
    24     OnClick = Button1Click
    25   end
    26   object Button2: TButton
    27     Left = 160
    28     Top = 24
    29     Width = 153
    30     Height = 25
    31     Caption = 'Button2_断开连接'
    32     TabOrder = 1
    33     OnClick = Button2Click
    34   end
    35   object Edit1: TEdit
    36     Left = 24
    37     Top = 176
    38     Width = 121
    39     Height = 21
    40     ImeName = '中文(简体) - 搜狗拼音输入法'
    41     TabOrder = 2
    42     Text = 'Edit1'
    43   end
    44   object Edit2: TEdit
    45     Left = 160
    46     Top = 176
    47     Width = 121
    48     Height = 21
    49     ImeName = '中文(简体) - 搜狗拼音输入法'
    50     TabOrder = 3
    51     Text = 'Edit2'
    52   end
    53   object Button3: TButton
    54     Left = 104
    55     Top = 96
    56     Width = 121
    57     Height = 25
    58     Caption = 'Button3_查询数据'
    59     TabOrder = 4
    60     OnClick = Button3Click
    61   end
    62   object Button4: TButton
    63     Left = 8
    64     Top = 128
    65     Width = 145
    66     Height = 25
    67     Caption = 'Button4_下一笔'
    68     TabOrder = 5
    69     OnClick = Button4Click
    70   end
    71   object Button5: TButton
    72     Left = 168
    73     Top = 128
    74     Width = 145
    75     Height = 25
    76     Caption = 'Button5_上一笔'
    77     TabOrder = 6
    78     OnClick = Button5Click
    79   end
    80   object Button6: TButton
    81     Left = 8
    82     Top = 216
    83     Width = 145
    84     Height = 25
    85     Caption = 'Button6_首笔数据'
    86     TabOrder = 7
    87     OnClick = Button6Click
    88   end
    89   object Button7: TButton
    90     Left = 168
    91     Top = 216
    92     Width = 145
    93     Height = 25
    94     Caption = 'Button7_末笔数据'
    95     TabOrder = 8
    96     OnClick = Button7Click
    97   end
    98 end

    --------Form结束

  • 相关阅读:
    mvc razor中renderPartial,RenderAction,Partial,Action的使用选择
    jquery最常用的几个方法。——可删除
    配置文件参数引用
    tinkphp5.0目录结构说明
    ionic ios上状态栏和app重叠解决方案
    cordova-plugin-alipay-v2使用篇(更新至20170725)(亲测可用)
    Ionic2 App Import BrowserAnimationsModule or NoopAnimationsModule问题
    ionic3.0 中带顶部导航的下拉刷新列表的实现
    npm install 时 提示err code EINTEGRITY报错
    ionic3.0 alipay-base插件移除后会添加多余的链接文件在nodes-modules中,导致再安装其他插件或移除插件时报错问题
  • 原文地址:https://www.cnblogs.com/dmqhjp/p/15152345.html
Copyright © 2011-2022 走看看