篆体字网 > 知识库

delphi2006

来源:篆体字网 2023-12-31 05:24:19 作者:篆字君

在FormCreate事件中:
SQLConnection1 :=TSQLConnection.Create(nil);
SQLConnection1.DriverName :="dbxmysql';
SQLConnection1.GetDriverFunc :="getSQLDriverMYSQL50';
SQLConnection1.LibraryName :="dbxopenmysql50.dll';
SQLConnection1.VendorLib :="libmysql.dll';
SQLConnection1.LoginPrompt :=false;
在此设置TSQLConnection的各个具体参数,当然也可以直接在组件属性面板中修改,或者修改dbxdrivers.ini中的对应参数,方法是多种的。
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';
设置数据库连接的各参数配置后,打开数据库连接,同时显示连接是否成功。
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;
查询表数据并在StringGrid中输出。
Disconnect按钮的事件:
if SQLConnection1.Connected=true then
SQLConnection1.Close;
FormDestroy事件:
if SQLConnection1.Connected=true then
SQLConnection1.Close;
SQLConnection1.Free;
运行程序,点击connect按钮,如果数据库连接成功可以看到success提示,然后点击query按钮就能查询到表中的数据。

上一篇:sexual partner

下一篇:onemoretime歌词

相关阅读