con:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=;Data Source=';//无数据库
Adoconnection1.ConnectionString:='';
Adoconnection1.ConnectionString:=con;
showmessage(con); try
adoconnection1.connected := true;
showmessage(连接成功);
except
showmessage(连接失败);
end;为什么总是显示‘连接成功’,用执行文件也如此。
到底如何判断有无ADO连上呢?
解决方案 »
- 求助:在Delphi上显示swf动画
- 一个多表查询语句
- 关于用idTcpXXXXX控件传输文件的问题
- Messagedlg和StringGrid的问题
- 救救我!“Delphi32发生错误(ADORTL60.BPL)。Delphi32即将关闭。如果仍有问题,请尝试重新启动计算机。”
- ClientSocket 再次连接的问题
- 如何提取ShellListView中多个文件名到列表框
- 如此菜的问题,高手就不要回答了.
- 哪位大虾能抽空帮我看一下这段图片显示、更改、删除的程序?
- 四舍五入的函数式是什么?
- VB中DATALIST控件和数据库绑定后 选定DATALIST中的一项点删除怎么才能把数据库中的相关数据一起删除 谢谢
- 关于delphi的代码问题 请指教
你加一名:Adoconnection1.connected := false;试试.
如果你要判断是否与数据库连接正常,你可以加一个查询系统表的查询,这样就能更准备的得到与数据库是否正常了.
Adoconnection1.ConnectionString:='';
Adoconnection1.ConnectionString:=con;
showmessage(con); try
adoconnection1.connected := False;
showmessage(连接成功);
except
showmessage(连接失败);
end; 结果还是‘连接成功’!!!
这一行代码可以完成对你指定的数据库的连接
adoconnection1.connected := False;
断开连接。只要你的数据库存在,或者没有什么系统内部的意外,都会成功执行,至少不会raise 错误,所以你的 except 部分根本就不会执行。adoconnection1.connected := true;
if adoconnection1.connected then
showmessage(连接成功)
else
showmessage(连接失败); 以上代码可能管用
showmessage(连接成功) =》showmessage('连接成功')
连接成功是正确的, 连接成功代表你和SQLServer取得了通讯。
你可以动态获取你要的数据库
接下来你就可以进行相关的数据库操作;
//-------------------------------
另外二楼的说把adoconnection1.connected := true; 中的true改成false,不知道你的本意是什么?
改成false后继续执行下去,当然也是'连接成功',因为已经连接上数据库了,程序会继续执行;
Adoconnection1.ConnectionString:=con;
showmessage(con);
adoconnection1.connected := False;
try
adoconnection1.connected := true;
//这里可以加一句查询系统表的语句
with adoqueyr1 do
begin
close;
sql.text := ' select getdate ';//sqlserver oracle这样写 select from dual';
open; //你看现在抛不抛异常
end;
showmessage('连接成功');
except
showmessage('连接失败');
end;
adoconnection1.connected := False;
showmessage(连接成功);
except
showmessage(连接失败);
end;
我晕,这样写,永远会显示连接成功,错误有其二.一adoconnection1.connected := False; 改为adoconnection1.connected := True;
二、showmessage(连接成功); 怎么字符串,‘’都没有了。
如果你这么写,就是连接失败的。
if adoconnection1.connected then
showmessage('连接成功')
else
showmessage('连接失败');
con:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=;Data Source=';//无数据库
Adoconnection1.ConnectionString:='';
Adoconnection1.ConnectionString:=con;
showmessage(con); try
adoconnection1.Connect;
if adoconnection1.Connected then
showmessage(连接成功)
except
showmessage(连接失败);
end; nnd,偶最讨厌代码不注意大小写影响市容。