我的做法是设计一个界面让客户输入数据库机器名,数据用户名,数据库密码.然后
保存在ini文件中.
下面是我的代码
rocedure TForm1.Button1Click(Sender: TObject);
var
connectstr,dbname,uname,pword,dirname:string;
filehandle:integer;
f:textfile;
begin
getdir(0,dirname);
if fileexists(dirname+'/linkdb.ini') then
begin
assignfile(f,dirname+'/linkdb.ini');
reset(f);
readln(f,dbname);
readln(f,uname);
readln(f,pword);
closefile(f);
end else
begin
dbname:=edit1.Text;
uname:= edit2.Text;
pword:= edit3.Text;
filehandle:=filecreate(dirname+'/linkdb.ini');
fileclose(filehandle);
assignfile(f,dirname+'/linkdb.ini');
rewrite(f);
dbname:='computername='+dbname;
uname:= 'username='+uname;
pword:= 'password='+pword;
writeln(f,dbname);
writeln(f,uname);
writeln(f,pword);
closefile(f);
end;
dbname:=copy(dbname,14,length(dbname)-13);
uname:= copy(uname,10,length(uname)-9);
pword:= copy(pword,10,length(pword)-9);
connectstr:='Provider=SQLOLEDB.1;Password='+pword+
';Persist Security Info=True;User ID='+uname+
';Initial Catalog=rsglxt;Data Source='+dbname+
';Use Procedure for Prepare=1;Auto Translate=True;'+
'Packet Size=4096;Workstation ID='+dbname;
try
adoconnection1.Connected:=false;
adoconnection1.ConnectionString:=connectstr;
adoconnection1.Connected:=true;
adotable1.Active:=true;
except
on E:exceptiondo
begin
adoconnection1.Connected:=false;
adotable1.Active:=false;
showmessage(e.Message);
// showmessage(inttostr(comparestr(e.Message,'[DBMSSOCN]一般性网络错误。请检查网络文档。')));
if e.Message='[DBMSSOCN]一般性网络错误。请检查网络文档。' then
begin
application.MessageBox('数据库机器名错误!'+#13+'请重新输入数据库机器名!','登录数据库错误',mb_ok+mb_iconstop+mb_applmodal);
deletefile(dirname+'/linkdb.ini');
end;
if e.Message='Cannot open database requested in login '+#39+'rsglxt'+#39+'. Login fails' then
application.MessageBox('数据库没有建立,登录数据库失败!'+#13+'请建立数据库!','登录数据库错误',mb_ok+mb_iconstop+mb_applmodal);
if e.Message='Login failed for user '+#39+uname+#39 then
begin
application.MessageBox('用户名或密码错误,登录数据库失败!'+#13+'请重新输入用户名或密码!','登录数据库错误',mb_ok+mb_iconstop+mb_applmodal);
deletefile(dirname+'/linkdb.ini');
end;
if e.Message='Login failed for user '+#39+'Administrator'+#39 then
begin
application.MessageBox('数据库机器名、用户名不能为空!'+#13+'请输入正确的数据库机器名、用户名或密码!','登录数据库错误',mb_ok+mb_iconstop+mb_applmodal);
deletefile(dirname+'/linkdb.ini');
end;
end;
end;
end;