动态赋值ADOconnection为什么没有数据

weixin_37016780 2017-11-04 11:59:24
代码片段:
s:= format('Provider=SQLOLEDB.1;Password=%s;Persist Security Info=True;User ID=%s;Initial Catalog=%s;Data Source=%s',[ps,usr,db,dbip]) ;
Adoconnection1.ConnectionString := s;

Adoconnection1动态从xml文档里获取到连接的参数

ADOQuery1的SQLstring里直接赋查询语句,Code 和Password 为参数

ADOQuery1.Parameters.ParamByName('Code').Value:= edtAccount.Text;
ADOQuery1.Parameters.ParamByName('Password').Value := trim(edtPassword.Text);
if ADOQuery1.Active then ADOQuery1.Close;
ADOQuery1.Open;
if ADOQuery1.RecordCount > 0 then ModalResult := mrOk;
end;
Result := ModalResult = mrOk;


访问某数据表,结果总是读不出来,相反,在窗体上直接EditConnectString连接数据库的方式却能读出来数据,好奇怪,求解答,谢谢!
...全文
445 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_37016780 2017-11-07
  • 打赏
  • 举报
回复
大家的回复都很有用,我认真又做了一遍就对了。代码如下
procedure TForm1.Button1Click(Sender: TObject);
var s:string; s1:string;
begin
    Adoconnection1.Connected:=False;
    label3.Caption:=GetcurrentDir();
    XMLDocument1.LoadFromFile(GetcurrentDir()+'\tmp.xml');
    dbip:=XMLDocument1.DocumentElement.ChildNodes[0].ChildNodes['dbip'].Text;
    db:=XMLDocument1.DocumentElement.ChildNodes[0].ChildNodes['db'].Text;
    ps:=XMLDocument1.DocumentElement.ChildNodes[0].ChildNodes['ps'].Text;
     usr:=XMLDocument1.DocumentElement.ChildNodes[0].ChildNodes['usr'].Text;
     s:=  format('Provider=SQLOLEDB.1;Password=%s;Persist Security Info=True;User ID=%s;Initial Catalog=%s;Data Source=%s',[ps,usr,db,dbip]) ;
     Adoconnection1.ConnectionString := s;

    Adoconnection1.Connected:=True;
     with Form1.ADOQuery1 do
      begin
      ADOQuery1.Close;
      ADOQuery1.SQL.Add('select * from SYS_Users where code =:Code and Password = :Password');
     ADOQuery1.Parameters.ParamByName('Code').Value:= edtAccount.Text;
     ADOQuery1.Parameters.ParamByName('Password').Value := trim(edtPassword.Text);
     ADOQuery1.Open;
      end;

end;
hongss 2017-11-07
  • 打赏
  • 举报
回复
Adoconnection有固定的连接方式,就像楼上说的,得按照 Adoconnection 的规矩来·~~~ 你在窗体上的控件内直接编辑时,你填入的ID,Pasword等内容,控件自己格式化成他需要的格式执行了
lyhoo163 2017-11-05
  • 打赏
  • 举报
回复
这是你没有连接,完整代码:

   ADOCon.Connected:=False;
   ADOCon.ConnectionString:='Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=数据管理;Data Source=LYHOO2';
   ADOCon.ConnectionString:=ConnectionString;
   ADOCon.Connected:=True;
Adoconnection.Connected:=True; 执行此句,控件原连接数据库。 在窗体上直接EditConnectString连接数据库的方式却能读出来数据,是因为窗体上点击时,窗体中运行了此句。直接连接上了。
日月路明 2017-11-04
  • 打赏
  • 举报
回复
首先,是否能正确连接到数据库,第二你的sql是否正确,第三, ADOQuery1.Parameters.ParamByName('Code').Value:= edtAccount.Text; ADOQuery1.Parameters.ParamByName('Password').Value := trim(edtPassword.Text); 给的值是否刚好没有匹配记录 重要的话说三遍: ADOQuery1.Open是否报错,ADOQuery1.Open是否报错,ADOQuery1.Open是否报错!

2,497

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧