很简单的一组代码,假设我们有一个名字叫db1.mdb的数据库,里面有一个叫做table1的表。然后代码如下:
[code=delphi]
unit Unit1;

interface

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

type
TForm1 = class(TForm)
conn: TADOConnection;
Button1: TButton;
DBGrid1: TDBGrid;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
try
self.conn.Close;
self.conn.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+sysutils.ExtractFilePath(application.ExeName)+
'db1.mdb;Persist Security Info=False';
self.conn.Connected:=true;
except on e:exception do
application.MessageBox(Pchar(e.Message),'提示',32);
end;
self.ADOQuery1.Active:=true;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
self.ADOQuery1.Active:=not self.ADOQuery1.Active;
end;

end.[/code]
窗体代码:
[code=delphi]
object Form1: TForm1
Left = 278
Top = 337
Width = 610
Height = 415
Caption = #38142#25509'acc'#31034#20363' Birdol.Com'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = #23435#20307
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
DesignSize = (
602
381)
PixelsPerInch = 96
TextHeight = 12
object Button1: TButton
Left = 32
Top = 16
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object DBGrid1: TDBGrid
Left = 8
Top = 48
Width = 577
Height = 297
Anchors = [akLeft, akTop, akRight, akBottom]
DataSource = DataSource1
TabOrder = 1
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -12
TitleFont.Name = #23435#20307
TitleFont.Style = []
end
object conn: TADOConnection
ConnectionString =
'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:\'#26368#36817'\Delphi'#23567#20363#23376'\lin' +
'kAccess\db1.mdb;Persist Security Info=False'
LoginPrompt = False
Mode = cmShareDenyNone
Provider = 'Microsoft.Jet.OLEDB.4.0'
Left = 128
end
object ADOQuery1: TADOQuery
Connection = conn
Parameters = <>
SQL.Strings = (
'select * from table1')
Left = 192
Top = 8
end
object DataSource1: TDataSource
DataSet = ADOQuery1
Left = 264
Top = 8
end
end[/code]

  One Response to “delphi连接access数据库示例”

  1. 你就不能简点写啊,说明白了啊,你以为那是wF啊,你写的真垃圾,以后别写。Angry[GRAVATAR=http://www.birdol.com]9dac45835fd5633a8627d68516e4617e[/GRAVATAR]

    nobird 于 2009-7-19 12:25:24 回复

    感谢提出意见,因为我对于delphi也是个初学者,所以代码难免有些繁杂,还望指教哈~

Sorry, the comment form is closed at this time.

   
© 2011 鸟儿博客 Suffusion theme by Sayontan Sinha