ADOConnection连接的问题。(50分)

  • 主题发起人 主题发起人 边城过客
  • 开始时间 开始时间

边城过客

Unregistered / Unconfirmed
GUEST, unregistred user!
现象:如果连接字符串正确,提示成功;如果连接字符串不对,就没出跳出连接失败的提示,在运行到“ADOConnection1.Open;”的时候直接出现:<br>Project Project1.exe raised exception class EOLeException with message '用户'sa'登陆失败。'Process stoped.Use Step or Run to continue. <br><br>源代码如下:<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; Screen.Cursor := crHourGlass;<br>&nbsp; &nbsp; &nbsp; Button2Click(nil);<br>&nbsp; &nbsp; &nbsp; if ADOConnection1.Connected then ADOConnection1.Close;<br>&nbsp; &nbsp; &nbsp; ADOConnection1.ConnectionString := 'Provider=SQLOLEDB.1;Persist Security Info=False;Initial Catalog=' + strDataBaseName;<br>&nbsp; &nbsp; &nbsp; if Trim(strServerName) &lt;&gt; '' then<br>&nbsp; &nbsp; &nbsp; &nbsp; ADOConnection1.ConnectionString := ADOConnection1.ConnectionString + ';Data Source=' + strServerName;<br>&nbsp; &nbsp; &nbsp; ADOConnection1.ConnectionString := ADOConnection1.ConnectionString + ';Password=' + strPassword + ';User ID=' + strUserName;<br>&nbsp; &nbsp; &nbsp; ADOConnection1.Open;<br>&nbsp; &nbsp; &nbsp; Application.MessageBox('连接成功 &nbsp; &nbsp; &nbsp; &nbsp;', '提示信息', MB_ICONINFORMATION);<br>&nbsp; &nbsp; &nbsp; ADOConnection1.Close;<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; Application.MessageBox('连接失败', '提示信息', MB_ICONERROR);<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; ADOConnection1.Close;<br>&nbsp; &nbsp; Screen.Cursor := crDefault;<br>&nbsp; end;<br>end;
 
给你一个比较通用的。<br>Function TForm1.ContoSQL(svr, uid, pwd, db: String): Boolean;<br>Const<br>&nbsp; constr='Provider=SQLOLEDB.1;Password=%s;'+<br>&nbsp; 'Persist Security Info=True;'+<br>&nbsp; 'User ID=%s;Initial Catalog=%s;Data Source=%s';<br>Begin<br>&nbsp; Result := False;<br>&nbsp; try<br>&nbsp; &nbsp; With ADOConnection1 Do<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; Connected := False;<br>&nbsp; &nbsp; &nbsp; ConnectionString := Format(constr,[pwd, uid, db, svr]);<br>&nbsp; &nbsp; &nbsp; LoginPrompt := False;<br>&nbsp; &nbsp; &nbsp; Connected := true;<br>&nbsp; &nbsp; &nbsp; Connected := False;<br>&nbsp; &nbsp; End;<br>&nbsp; &nbsp; Result := True;<br>&nbsp; except On E: Exception Do<br>&nbsp; &nbsp; Application.MessageBox(PChar(E.Message),'连接数据库失败!',16);<br>&nbsp; end;<br>End;<br><br><br>procedure TForm1.BitBtn1Click(Sender: TObject);<br>begin<br>&nbsp; If ContoSQL('127.0.0.1', 'sa', 'sa', 'Northwind') Then<br>&nbsp; Application.MessageBox(PChar('ok'),'系统提示',64);<br>end;
 
你直接运行程序就行了阿,连接不上数据时提示连接失败不就行了吗/
 
直接运行程序,按道理当连接不上时提示连接失败,返回程序。<br>但现在是不提示连接失败,直接报错,然后中止了程序。
 
你是单独运行程序,还是在Delphi环境下运行啊?<br>还有,为啥要写下面的语句?<br>finally<br>&nbsp; &nbsp; ADOConnection1.Close;
 
ADOConnection1.Close;<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; Application.MessageBox('连接失败', '提示信息', MB_ICONERROR);<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; ADOConnection1.Close;<br>&nbsp; &nbsp; Screen.Cursor := crDefault;<br>&nbsp; end;<br>---------------------------------------连接成功后最好不要CLOSE<br><br>这种问题一般是连接时数据库名或密码不正确,你自己最好先用可视连接,测试成功后再把连接字符中的动态配置的东西加进去.
 
编译后双击生成的exe文件就会提示'连接失败',<br>你用delphi直接运行肯定就没出跳出连接失败的提示,在运行到“ADOConnection1.Open;”的时候直接出现:<br>Project Project1.exe raised exception class EOLeException with message '用户'sa'登陆失败。'Process stoped.Use Step or Run to continue.
 
接受答案了.
 
后退
顶部