1)设置ODBC中, Advanced-->Flag 3-->Read Options From my.cnf
2)将以下内容保存为%windir%\my.cnf(注意:是WINDOWS系统目录)
你可以将下面的内容复制保存成文件即可.
[client]
default-character-set=gbk(gb2312)
以上是利用一个配置文件进行的处理.在发布程序的时候比较难处理.
---------------------------------------------------------------------------------------
下面这个方法不用利用配置文件,可以在创建完数据连接之后执行.
//"SET NAMES GB2312"的作用相当于
//SET character_set_client = GB2312
//SET character_set_results = GB2312
//SET character_set_connection = GB2312
ADOQuery1.Close()
ADOQuery1.SQL.Text := 'SET NAMES GB2312'
ADOQuery1.ExecSQL()
ADOQuery1.Close()
ADOQuery1.SQL.Text := 'SET character_set_client = GB2312'
ADOQuery1.ExecSQL()
//当将此句注释掉得话,从数据库中读取的内容为乱码
ADOQuery1.Close()
ADOQuery1.SQL.Text := 'SET character_set_results = GB2312'
ADOQuery1.ExecSQL()
//当将此句注释掉的话,存入数据库的内容变为乱码
ADOQuery1.Close()
ADOQuery1.SQL.Text := 'SET character_set_connection = GB2312'
ADOQuery1.ExecSQL()
Delphi6新增的DBExpress专门用来对付Mysql,DB2,Interbase,Oracle等数据库,使用时注意将libmysql.dll拷贝到当前目录或系统目录(98:system,NT:system32)下,并保证您的3306端口与远程服务器是相通的。
源程序:
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DBXpress, DB, SqlExpr, StdCtrls, ComCtrls, FMTBcd, Grids,
DBGrids, Provider, DBClient, DBLocal, DBLocalS, DBTables
type
TForm1 = class(TForm)
SQLConnection: TSQLConnection
StatusBar1: TStatusBar
Label1: TLabel
DataSource1: TDataSource
DBGrid1: TDBGrid
GroupBox1: TGroupBox
Label2: TLabel
Password: TEdit
User_Name: TEdit
HostName: TEdit
Label3: TLabel
Label4: TLabel
Button1: TButton
GroupBox2: TGroupBox
Label5: TLabel
ESQL: TEdit
Label6: TLabel
Database: TEdit
ButtonGo: TButton
SQLClientDataSet: TSQLClientDataSet
procedure Button1Click(Sender: TObject)
procedure ButtonGoClick(Sender: TObject)
procedure FormClose(Sender: TObjectvar Action: TCloseAction)
private
{ Private declarations }
public
{ Public declarations }
end
var
Form1: TForm1
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject)
begin
with SQLConnection do
begin
Close
Params.Values['HostName']:=HostName.Text
Params.Values['UserName']:=User_Name.Text
Params.Values['Password']:=Password.Text
Params.Values['Database']:=Database.Text
try
Connected:=True
Statusbar1.Panels[0].Text:='Connect OK'
ButtonGo.Enabled:=True
except
MessageDlg('Connect Error',mtError,[mbyes],0)
end
end
end
procedure TForm1.ButtonGoClick(Sender: TObject)
begin
with SQLClientDataSet do
begin
Close
CommandText:=ESQL.Text
Open
end
end
procedure TForm1.FormClose(Sender: TObjectvar Action: TCloseAction)
begin
SQLConnection.Close
end
end.
从错误提示看,使用的是 MySql 数据库,错误原因是由于 SQL 查询语句的语法错误,错误位置在 [zddw_jbxx] 附近。
建议:
将 [zddw_jbxx] 修改为 zddw_jbxx,即去掉 []。
2. 先将生成后 sql 语句,到 mysql 里先执行一下,如果正确再使用。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)