delphi演示程序--ado操作Access数据库  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi演示程序--ado操作Access数据库


源代码:
---------------------------------------------------------------------------------------------

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, DB, ADODB, Grids, DBGrids, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    Button2: TButton;
    Button1: TButton;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    ADOConnection1: TADOConnection;
    ADOQuery1: TADOQuery;
    PopupMenu1: TPopupMenu;
    Label3: TLabel;
    edt3: TEdit;
    Label4: TLabel;
    edt4: TEdit;
    btn3: TButton;
    btn4: TButton;
    ADOQuery2: TADOQuery;
    ADOQuery3: TADOQuery;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedure DBGrid1CellClick(Column: TColumn);
    procedure btn4Click(Sender: TObject);
 
  Private
   id: Integer;
    { Private declarations }
  public
    constructor Create(AOwner: TComponent); override;

    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
begin
  ADOQuery2.Close;
  ADOQuery2.Open;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('insert into tbl1 (name,include,tim) values (:name,:include,now())');
    Parameters.FindParam('name').Value := edit1.Text;
    Parameters.FindParam('include').Value := edit2.Text;
    ExecSQL;
  end;
end;

procedure TForm1.btn3Click(Sender: TObject);
begin
 if id > 0 then
  begin
    try
      with  ADOQuery3 do
      begin
        Close;
        SQL.Clear;
        SQL.Add('delete from tbl1 where id = :id');
        Parameters.FindParam('id').Value := id;
        ExecSQL;
      end;
      ShowMessage('删除完成!!');
    except
      ShowMessage('有异常!!');
    end;
  end
  else
    ShowMessage('请先选择选择');
end;

constructor TForm1.Create(AOwner: TComponent);
begin
  inherited;
  id:=0;
end;

procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
with ADOQuery2 do
  begin
    id := FieldByName('id').AsInteger;
    edt3.Text := FieldByName('name').AsString;
    edt4.Text := FieldByName('include').AsString;
  end;
end;

procedure TForm1.btn4Click(Sender: TObject);
begin
  if id > 0 then
  begin
    try
      with ADOQuery1 do
      begin
        Close;
        SQL.Clear;
        SQL.Add('UPDATE tbl1 SET name = :name,include = :include,tim = now() WHERE id = :id');
        Parameters.FindParam('name').Value := edt3.Text;
        Parameters.FindParam('include').Value := edt4.Text;
        Parameters.FindParam('id').Value := id;
        ExecSQL;
      end;
      ShowMessage('修改完成!!');
    except
      ShowMessage('有异常!!');
    end;
  end
  else
    ShowMessage('请先选择!');
end;

end.
 

界面代码:
---------------------------------------------------------------------------------

object Form1: TForm1
  Left = 802
  Top = 459
  Width = 428
  Height = 313
  Caption = 'ADO操作ACCESS数据库  猪悟能'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 32
    Top = 24
    Width = 42
    Height = 13
    Caption = '标题:'
  end
  object Label2: TLabel
    Left = 32
    Top = 48
    Width = 42
    Height = 13
    Caption = '内容:'
  end
  object Label3: TLabel
    Left = 31
    Top = 226
    Width = 59
    Height = 13
    AutoSize = False
    Caption = '标题:'
  end
  object Label4: TLabel
    Left = 31
    Top = 250
    Width = 59
    Height = 13
    AutoSize = False
    Caption = '内容:'
  end
  object Edit1: TEdit
    Left = 80
    Top = 21
    Width = 121
    Height = 21
    TabOrder = 0
  end
  object Edit2: TEdit
    Left = 80
    Top = 45
    Width = 121
    Height = 21
    TabOrder = 1
  end
  object Button2: TButton
    Left = 248
    Top = 48
    Width = 129
    Height = 25
    Caption = '查看数据库列表'
    TabOrder = 2
    OnClick = Button2Click
  end
  object DBGrid1: TDBGrid
    Left = 32
    Top = 88
    Width = 353
    Height = 120
    DataSource = DataSource1
    TabOrder = 3
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
    OnCellClick = DBGrid1CellClick
  end
  object edt3: TEdit
    Left = 101
    Top = 222
    Width = 215
    Height = 21
    TabOrder = 4
  end
  object edt4: TEdit
    Left = 101
    Top = 250
    Width = 215
    Height = 21
    TabOrder = 5
  end
  object btn3: TButton
    Left = 330
    Top = 222
    Width = 75
    Height = 21
    Caption = '删除'
    TabOrder = 6
    OnClick = btn3Click
  end
  object btn4: TButton
    Left = 330
    Top = 242
    Width = 75
    Height = 21
    Caption = '修改'
    TabOrder = 7
    OnClick = btn4Click
  end
  object Button1: TButton
    Left = 248
    Top = 16
    Width = 129
    Height = 25
    Caption = '添加到数据库'
    TabOrder = 8
    OnClick = Button1Click
  end
  object DataSource1: TDataSource
    DataSet = ADOQuery2
    Left = 208
    Top = 8
  end
  object ADOConnection1: TADOConnection
    ConnectionString =
      'Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin;Data ' +
      'Source=F:\我的文档\桌面\delph代码\演示程序--数据库编程\adodemo.mdb;Mode=Share Den' +
      'y None;Extended Properties="";Persist Security Info=True;Jet OLE' +
      'DB:System database="";Jet OLEDB:Registry Path="";Jet OLEDB:Datab' +
      'ase Password="";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locki' +
      'ng Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global B' +
      'ulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:' +
      'Create System Database=False;Jet OLEDB:Encrypt Database=False;Je' +
      't OLEDB:Don''t Copy Locale on Compact=False;Jet OLEDB:Compact Wit' +
      'hout Replica Repair=False;Jet OLEDB:SFP=False'
    Provider = 'Microsoft.Jet.OLEDB.4.0'
    Left = 208
    Top = 56
  end
  object ADOQuery1: TADOQuery
    Connection = ADOConnection1
    Parameters = <>
    Left = 384
    Top = 16
  end
  object PopupMenu1: TPopupMenu
    Left = 248
    Top = 136
  end
  object ADOQuery2: TADOQuery
    Connection = ADOConnection1
    Parameters = <>
    SQL.Strings = (
      'select * from tbl1')
    Left = 328
    Top = 120
  end
  object ADOQuery3: TADOQuery
    Connection = ADOConnection1
    Parameters = <>
    Left = 304
    Top = 208
  end
end


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.04495906829834 seconds