在下面這個delphi連接以及操作mysql數據庫中,我們以最簡單展示,沒有考慮異常等情況,我在其中加了兩個按鈕,一個是連接數據庫,一個是從連接的數據庫查詢一個數據,
源碼下載地址:http://download.csdn.net/detail/cq20110310/6854069
mysql:表student,字段name
unit uTest1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, DB, ADODB, StdCtrls; type TFormDB = class(TForm) btn1: TButton; Button1: TButton; ADOConn: TADOConnection; ADOQuery:TADOQuery; procedure btn1Click(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var FormDB: TFormDB; implementation {$R *.dfm} procedure TFormDB.btn1Click(Sender: TObject); var strConn:string; begin ADOConn:=TADOConnection.Create(Self); strConn:='DRIVER={MySQL ODBC 5.1 Driver};SERVER=127.0.0.1;DATABASE=delphi_test1;UID=root;PASSWORD=OMSDB;OPTION=3;PORT=3306;'; ADOConn.ConnectionString:=strConn; ADOConn.Open; if ADOConn.Connected then begin ShowMessage('連接數據庫成功'); end else begin ShowMessage('連接數據庫失敗'); end; end; procedure TFormDB.Button1Click(Sender: TObject); var strSQL:string; begin ADOQuery:=TADOQuery.Create(Self); ADOQuery.Connection:=ADOConn; strSQL:='select * from student '; ADOQuery.SQL.Add(strSQL); ADOQuery.Open; ShowMessage(ADOQuery.FieldByName('name').AsString); end; end.