我是通过INI连接SQL2000数据库的,以下是代码:(但是运行时,第一次点button1正常,但当第二次点button1的时候,就会报错:打开对象时不允许操作...请问怎么处理呢?是什么原因?初学,谢谢)
procedure TForm1.Button1Click(Sender: TObject);
var
FileName,ServerIP,SQLDBName,SQLUserName,SQLPwd:string;
begin
FileName:=ExtractFilePath(application.ExeName)+'conf.ini';
with TInifile.Create(Filename) do
begin
ServerIP:=ReadString('ServerInfo','ServerIP','');
SQLDBName:=ReadString('ServerInfo','SQLDBName','');
SQLUserName:=ReadString('ServerInfo','SQLUserID','');
SQLPwd:=ReadString('ServerInfo','SQLPwd','');
Destroy;
end;
ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='+SQLPwd+
';Persist Security Info=True;'+'User ID='+SQLUserName+';Initial Catalog='
+SQLDBName+';Data Source='+ServerIP;
aa.Close;
aa.SQL.Clear;
aa.SQL.Add('select * from T');
aa.Open;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
FileName,ServerIP,SQLDBName,SQLUserName,SQLPwd:string;
begin
FileName:=ExtractFilePath(application.ExeName)+'conf.ini';
with TInifile.Create(Filename) do
begin
ServerIP:=ReadString('ServerInfo','ServerIP','');
SQLDBName:=ReadString('ServerInfo','SQLDBName','');
SQLUserName:=ReadString('ServerInfo','SQLUserID','');
SQLPwd:=ReadString('ServerInfo','SQLPwd','');
Destroy;
end;
ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='+SQLPwd+
';Persist Security Info=True;'+'User ID='+SQLUserName+';Initial Catalog='
+SQLDBName+';Data Source='+ServerIP;
aa.Close;
aa.SQL.Clear;
aa.SQL.Add('select * from T');
aa.Open;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
FileName,ServerIP,SQLDBName,SQLUserName,SQLPwd:string;
begin
FileName:=ExtractFilePath(application.ExeName)+'conf.ini'; with TInifile.Create(Filename) do
begin
ServerIP:=ReadString('ServerInfo','ServerIP','');
SQLDBName:=ReadString('ServerInfo','SQLDBName','');
SQLUserName:=ReadString('ServerInfo','SQLUserID','');
SQLPwd:=ReadString('ServerInfo','SQLPwd','');
Free;
end;
ADOConnection1.close;
ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='+SQLPwd+
';Persist Security Info=True;'+'User ID='+SQLUserName+';Initial Catalog='
+SQLDBName+';Data Source='+ServerIP;
try
ADOConnection1.open;
aa.Close;
aa.connection := ADOConnection1;
aa.SQL.Clear;
aa.SQL.Add('select * from T');
aa.Open;
except
raise;
end;
end;
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
';Persist Security Info=True;'+'User ID='+SQLUserName+';Initial Catalog='
+SQLDBName+';Data Source='+ServerIP;
前面加 ADOConnection1.close;
form.onshow:
bgein
Button1.visible:=false;
Button1Click;
end;
就可以了.
或在你主窗口formcreate的时候 设置数据库连接
在FormCreate里加上
FileName:=ExtractFilePath(application.ExeName)+'conf.ini'; with TInifile.Create(Filename) do
begin
ServerIP:=ReadString('ServerInfo','ServerIP','');
SQLDBName:=ReadString('ServerInfo','SQLDBName','');
SQLUserName:=ReadString('ServerInfo','SQLUserID','');
SQLPwd:=ReadString('ServerInfo','SQLPwd','');
Free;
end;
ADOConnection1.close;
ADOConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='+SQLPwd+
';Persist Security Info=True;'+'User ID='+SQLUserName+';Initial Catalog='
+SQLDBName+';Data Source='+ServerIP;
在
procedure TForm1.Button1Click(Sender: TObject);
begin
aa.Close;
aa.SQL.Clear;
aa.SQL.Add('select * from T');
aa.Open;
end;