请问在DELPHI中如何立即断开ADO连接??? ( 积分: 50 )

  • 主题发起人 主题发起人 Flashcqxg
  • 开始时间 开始时间
我将我的代码贴出来,大家帮我看看吧,我的疑问都写在注释里面了,谢谢大家:<br>unit&nbsp;Unit1;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Variants,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,<br>&nbsp;&nbsp;Dialogs,&nbsp;StdCtrls,&nbsp;DB,&nbsp;ADODB;<br><br>type<br>&nbsp;&nbsp;TForm1&nbsp;=&nbsp;class(TForm)<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOConnection1:&nbsp;TADOConnection;<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1:&nbsp;TADOQuery;<br>&nbsp;&nbsp;&nbsp;&nbsp;Button1:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;Button2:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button1Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button2Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Public&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;end;<br><br>var<br>&nbsp;&nbsp;Form1:&nbsp;TForm1;<br><br>implementation<br><br>{$R&nbsp;*.dfm}<br><br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);<br>begin<br><br>&nbsp;&nbsp;if&nbsp;ADOConnection1.Connected&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;ShowMessage('连接上了的')<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;ShowMessage('将连接数据库');<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOConnection1.ConnectionString&nbsp;:=&nbsp;'Provider=MSDASQL.1;Password=sq;Persist&nbsp;Security&nbsp;Info=True;User&nbsp;ID=sa;Data&nbsp;Source=test';<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOConnection1.KeepConnection&nbsp;:=&nbsp;True;<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOConnection1.Open;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Connection&nbsp;:=&nbsp;ADOConnection1;<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Close;<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.SQL.Clear;<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.SQL.Add('select&nbsp;*&nbsp;from&nbsp;sys_users');<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Open;<br>&nbsp;&nbsp;&nbsp;&nbsp;ShowMessage(IntToStr(ADOQuery1.RecordCount));<br>&nbsp;&nbsp;&nbsp;&nbsp;ADOQuery1.Close;<br>&nbsp;&nbsp;end;<br><br>end;<br><br>procedure&nbsp;TForm1.Button2Click(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;ADOQuery1.Connection&nbsp;:=&nbsp;nil;<br>&nbsp;&nbsp;ADOQuery1.Active&nbsp;:=&nbsp;False;<br>&nbsp;&nbsp;ADOConnection1.Close;<br>&nbsp;&nbsp;ADOConnection1.KeepConnection&nbsp;:=&nbsp;False;<br>//如果这里真正断开了,那么下面的语句应该出错才对呀?<br><br>&nbsp;&nbsp;ADOQuery1.Connection&nbsp;:=&nbsp;ADOConnection1;<br>&nbsp;&nbsp;ADOQuery1.Close;<br>&nbsp;&nbsp;ADOQuery1.SQL.Clear;<br>&nbsp;&nbsp;ADOQuery1.SQL.Add('select&nbsp;*&nbsp;from&nbsp;sys_users');<br>&nbsp;&nbsp;ADOQuery1.Open;<br>&nbsp;&nbsp;ShowMessage(IntToStr(ADOQuery1.RecordCount));<br>&nbsp;&nbsp;//但这里又有正确的结果?????<br><br>end;<br><br>end.
 
ADOQuery1.Open;<br>这句就会调用ADOQuery1.Connection.Open;所以就连上了<br><br>&nbsp;&nbsp;ADOConnection1.Close;<br>在这句后面加上<br>&nbsp;&nbsp;ADOConnection1.ConnectionString&nbsp;:=&nbsp;'';也不行?
 
兄弟看一下你的数据集的&nbsp;connectstring&nbsp;是不是有代码。
 
ADOConnection1.ConnectionString&nbsp;:=&nbsp;'';也不行?&nbsp;<br><br>是的,加上了也不行。<br><br>兄弟看一下你的数据集的&nbsp;connectstring&nbsp;是不是有代码。<br>检查了,没有代码。
 
多人接受答案了。
 
后退
顶部