try
ADOQuery1.ConnectionString:='Provider=SQLOLEDB.1;Password=123456'
+';Persist Security Info=True;User ID=sa'
+';Initial Catalog=test'
+';Data Source=192.168.0.3';
except
showmessage('对不起!数据库连接错误!');
end;我这样写try根本不起作用。如果把IP或用户名给改成错的,程序就会死掉。哪位高手能帮我解决这个问题
ADOQuery1.ConnectionString:='Provider=SQLOLEDB.1;Password=123456'
+';Persist Security Info=True;User ID=sa'
+';Initial Catalog=test'
+';Data Source=192.168.0.3';
except
showmessage('对不起!数据库连接错误!');
end;我这样写try根本不起作用。如果把IP或用户名给改成错的,程序就会死掉。哪位高手能帮我解决这个问题
编译后运行exe文件试试
try
ADOQuery1.ConnectionString:='Provider=SQLOLEDB.1;Password=123456'
+';Persist Security Info=True;User ID=sa'
+';Initial Catalog=test'
+';Data Source=192.168.0.3';
ADOQuery1.SQL := 'select 1';
ADOQuery1.open;
except
showmessage('对不起!数据库连接错误!');
end;
我怀疑try except 为什么不起作用!希望有人教我另外一种方法。
我就直接用ADOQuery心里上感觉应该对程序执行效率会有提高吧
你用adoquery连接也是走的联接串,是一样的,但当你有多个query时还会这样写吗?
1.增加了联接数
2.代码重复
ConnType, ConnTime: integer; var RetString: widestring): integer;
var rel: Byte;
AdoConn: TADOConnection;
ConnStr: string;
begin
rel := 0;
try
begin
AdoConn := TADOConnection.Create(nil);
if Conntype = 1 then
Connstr := 'Provider=SQLOLEDB.1;password=' + Pwd + ';Persist Security Info=true;User ID='
+ Uid + ';Initial Catalog=' + DbName + ';Data Source=' + sqlName + ';application name=' + Appname
else
Connstr := 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog='
+ dbname + ';Data Source=' + sqlName + ';application name=' + Appname;
with AdoConn do
begin
ConnectionTimeout := ConnTime;
CommandTimeout := ConnTime;
loginprompt := false;
keepconnection := true;
connectionstring := Connstr;
Provider := 'SQLOLEDB.1';
DefaultDatabase := DBName;
Connected := true;
Connected := False;
Free;
end;
end
except
on e: exception do
begin
Retstring := e.message;
AdoConn.free;
rel := 1;
end;
end;
Result := rel;
end;
一个adoconnection连接数据库,N个adoquery再与adoconnection相连LZ觉得那个效率更高呢?