(1)通过ODBC+ADO的方式连:

with ADOCon1 do

begin

Close;

ConnectionString:=( 'DRIVER={MySQL ODBC 5.1 Driver}; '+

'SERVER=192.168.1.107; '+

'DATABASE=test; '+

'USER=root; '+

'PASSWORD=root; '+

'PORT=3406; '+

'OPTION=3; ');

try

Open;

application.MessageBox( '连接成功!', '提示 ',MB_ICONINFORMATION);

except

application.MessageBox( '无法连接数据库服务器.请与管理员联系 ', '提示 ',MB_ICONINFORMATION);

end;  //try

end;  //with

(2)通过DBExpress连:

SQLConnection1.Connected:= false;

with SQLConnection1.Params do

begin

Text:='DriverName=MSSQL'+#13+

'ServerPort=' + 3406 + #13+

'HostName=' + '192.168.1.107'+ #13+

'DataBase= test' +#13+

'User_Name=root'+#13+

'Password=root'+#13+

'BlobSize=-1'+#13+

'ErrorResourceFile='+#13+

'LocaleCode=0000'+#13+

'ServerCharset=gb2312'+#13+

'MSSQL   TransIsolation=ReadCommited'+ #13+

'OS   Authentication=False';

end;

try

SQLConnection1.Connected:=true;

application.MessageBox( '连接成功!', '提示 ',MB_ICONINFORMATION);

except

application.MessageBox( '无法连接数据库服务器.请与管理员联系 ', '提示 ',MB_ICONINFORMATION);

end;

注:在delphi7及以下版本,ServerPort设置无效

(3)通过MyDAC连:

with ADOCon1 do

begin

Close;

Server:= '192.168.1.107';

Port:= 3406;

Username:= 'root';

Password:= 'root';

Database:= 'test';

Options.Charset:= 'gb2312';

try

Open();

application.MessageBox( '连接成功!', '提示 ',MB_ICONINFORMATION);

except      application.MessageBox( '无法连接数据库服务器.请与管理员联系 ', '提示 ',MB_ICONINFORMATION);    end;  //try  end;   //with

Logo

一站式 AI 云服务平台

更多推荐