2.DBExpress+dbxopenmysql50.dll
可能很多人會奇怪,dbxopenmysql50.dll是什么東東?DBExpress不就是數(shù)據(jù)庫連接組件了嗎,為什么還要加上這個(gè)東西?這是由于Delphi2006中的DBExpress對Mysql高版本的支持很差,從國外論壇上看到的說法似乎是根本就沒實(shí)現(xiàn),所以說雖然TSQLConnection組件中提供了Mysql選項(xiàng),但直接使用的話是不行的(低版本的mysql可能可以),我遇到的現(xiàn)象是提示“Unable to Load libmysql.dll”,但其實(shí)我已經(jīng)在系統(tǒng)目錄System32下、Delphi安裝目錄的bin中、開發(fā)工程項(xiàng)目文件夾中都安放了該文件,還是找不到該dll。 dbxopenmysql50.dll是由老外開發(fā)的,而且開源,還是老外好啊,可以到如下網(wǎng)址去下載: http://www./delphi/dbexpress_and_mysql_5.html 使用時(shí)需要將dbxopenmysql50.dll和libmysql.dll都放到工程文件夾下。順便提一下,libmysql.dll在mysql安裝目錄下的\lib\opt目錄中。 使用方法有兩種,一種是直接修改Borland\BDS\4.0\dbExpress下的dbxdrivers.ini,調(diào)整其中關(guān)于mysql的各參數(shù)。 另一種就是在程序中指定,現(xiàn)在我以這種方式為例說明這種連接方式的開發(fā)方法。 在Form上放上TSQLConnection、TSQLQuery、TStringGrid、3個(gè)TButton、TLable。界面顯示如下圖。 在FormCreate事件中:
SQLConnection1 := TSQLConnection.Create(nil); SQLConnection1.DriverName := 'dbxmysql'; SQLConnection1.GetDriverFunc := 'getSQLDriverMYSQL50'; SQLConnection1.LibraryName := 'dbxopenmysql50.dll'; SQLConnection1.VendorLib := 'libmysql.dll'; SQLConnection1.LoginPrompt := false; 在此設(shè)置TSQLConnection的各個(gè)具體參數(shù),當(dāng)然也可以直接在組件屬性面板中修改,或者修改dbxdrivers.ini中的對應(yīng)參數(shù),方法是多種的。 Connect按鈕的事件: SQLConnection1.Params.Append('Database=user'); SQLConnection1.Params.Append('User_Name=mysql'); SQLConnection1.Params.Append('Password=mysql'); SQLConnection1.Params.Append('HostName=localhost'); SQLConnection1.Open; if SQLConnection1.Connected = true then Label1.Caption := 'success' else Label1.Caption := 'fail'; 設(shè)置數(shù)據(jù)庫連接的各參數(shù)配置后,打開數(shù)據(jù)庫連接,同時(shí)顯示連接是否成功。 Query按鈕的事件: var i,j: Integer; begin SQLQuery1.SQL.Clear; SQLQuery1.SQL.Add('SELECT * FROM userinfo'); SQLQuery1.Active := true; i := 0; SQLQuery1.First; while not SQLQuery1.eof do begin for j:=0 to SQLQuery1.FieldCount-1 do StringGrid1.cells[j,i]:=SQLQuery1.Fields[j].AsString; SQLQuery1.next; inc(i); end; SQLQuery1.Active := false; 查詢表數(shù)據(jù)并在StringGrid中輸出。 Disconnect按鈕的事件: if SQLConnection1.Connected = true then SQLConnection1.Close; FormDestroy事件: if SQLConnection1.Connected = true then SQLConnection1.Close; SQLConnection1.Free; 運(yùn)行程序,點(diǎn)擊connect按鈕,如果數(shù)據(jù)庫連接成功可以看到success提示,然后點(diǎn)擊query按鈕就能查詢到表中的數(shù)據(jù)。 本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/freewind88/archive/2007/01/15/1483709.aspx
本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/freewind88/archive/2007/01/15/1483709.aspx
|
|