首先在窗體上拖拽sqlconnection和sqlquery兩個控件:
然后在測試連接中,寫入以下代碼(注意exe生成目錄下需要有dbxopenmysql50.dll和libmysql.dll)
-
SQLConnection1 := TSQLConnection.Create( nil);
-
SQLConnection1.DriverName := 'dbxmysql';
-
SQLConnection1.GetDriverFunc := 'getSQLDriverMYSQL50';
-
SQLConnection1.LibraryName := 'dbxopenmysql50.dll';
-
SQLConnection1.VendorLib := 'libmysql.dll';
-
SQLConnection1.LoginPrompt := false;
-
SQLConnection1.Params.Append( 'Database=sampledb31');
-
SQLConnection1.Params.Append( 'User_Name=test');
-
SQLConnection1.Params.Append( 'Password=test');
-
SQLConnection1.Params.Append( 'HostName=192.168.1.78');
-
-
SQLConnection1.Open;
-
if SQLConnection1.Connected = true then
-
begin
-
SQLQuery1.SQLConnection := SQLConnection1;
-
SQLQuery1.SQL.Clear;
-
SQLQuery1.SQL.Text := 'SET NAMES gbk;';//設置mysql查詢中文不亂碼
-
SQLQuery1.ExecSQL();
-
MessageBox( 0,'連接成功','提示',MB_ICONASTERISK and MB_ICONINFORMATION);
-
end else
-
begin
-
showmessage( '數據庫連接失敗!');
-
SQLConnection1.Close;
-
end;