首先在窗体上拖拽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;