帮忙看一下,下面的代码错在哪里?首先读取配置文件pragram.ini中的数据库配置参数,然后,将一个表的总数分配给edit1.text,还想取一个字段值给edit2.text:begin
 try   //判断连接的语句,首先读取配置文件-待修改
  myinifile:=Tinifile.Create(ExtractFilePath(paramstr(0))+'pragram.ini');
  password:=myinifile.readstring('setup','user_pwd','');
  user:=myinifile.readstring('setup','user_name','');
  database:=myinifile.readstring('setup','srv_data','');
  server:=myinifile.readstring('setup','data_source','');
  adoconnection1.Close;
  adoconnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='
  +password+';Persist Security Info=True;User ID='
  +user+';Initial Catalog='+database
  +';Data Source='+server;
  adoconnection1.Open;
  if adoconnection1.Connected then    //判断是否连接成功
  begin
    application.MessageBox('连接成功!','提示');
    adoquery1.SQL.Add('select count(IDH) from rc');
    edit1.Text:=adoquery1.Fields[8].AsString;
    adoconnection1.Close; //关闭
  end;
  except
    application.MessageBox('数据库连接失败,请确认你已配置好服务器','提示');
    frmserver.ShowModal;   //这里也提示错的
  end;
 end;
还有,我想将配置文件内的内容加密后再加入,该怎么改:
 begin
  myinifile:=Tinifile.Create(ExtractFilePath(paramstr(0))+'pragram.ini');
  myinifile.WriteString('setup','Data_Source',edit1.Text);
  myinifile.WriteString('setup','srv_data',edit2.Text);
  myinifile.WriteString('setup','user_name',edit3.Text);
  myinifile.WriteString('setup','user_pwd',edit4.Text);
  myinifile.Destroy;
end;
急等!!

解决方案 »

  1.   

    edit1.Text:=adoquery1.Fields[8].AsString;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    edit1.Text:=adoquery1.Fields[0].AsString;
      

  2.   

    这样试试,'select count(IDH) from rc' 改成 
    select count(idh) as idh from rc 
    edit1.text:=adoquery1.fieldbyname('idh').asstring;
    把adoconnection1 的close,open改成.Connected:=false;.Connected:=true;
    多试几遍啊~
      

  3.   

    //////////////////////
    表的总数分配给edit1.text,
    edit1.text:=IntToStr(ADOQuery1.RecordCount)
    //////////////////////////////////
    还想取一个字段值给edit2.text:
    应该为:
    edit2.text:=ADOQuery1.Fields[0].AsString;/////////////////////////////
    application.MessageBox('数据库连接失败,请确认你已配置好服务器','提示');
        frmserver.ShowModal;   //这里也提示错的应改为:
       application.MessageBox('数据库连接失败,请确认你已配置好服务器','提示');
    frmserver:=TfrmServer.Create(Application);
        frmserver.ShowModal;   //这里也提示错的
      

  4.   

    我首先编译运行后提示,连接成功,然后跳出错误提示(标题:Debugger Exception Notitication)是:
     Project Project1.exe raised exception class EDatabaseError with message'ADOQuery1:field'idh' not found',Use Step or Run to continue.
      我不改动任何代码,再编译就提示:数据库连接失败,请确认你已配置好服务器.
      

  5.   

    myjerry(网络猎人) :根据你的代码,我改成以下样子:procedure TForm1.Button1Click(Sender: TObject);
    var
     server:string;
     database:string;
     password:string;
     user:string;
    begin
     try   //判断连接的语句,首先读取配置文件-待修改
      myinifile:=Tinifile.Create(ExtractFilePath(paramstr(0))+'setup.ini');
      password:=myinifile.readstring('setup','user_pwd','');
      user:=myinifile.readstring('setup','user_name','');
      database:=myinifile.readstring('setup','srv_data','');
      server:=myinifile.readstring('setup','data_source','');
      adoconnection1.Close;
      adoconnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='
      +password+';Persist Security Info=True;User ID='
      +user+';Initial Catalog='+database
      +';Data Source='+server;
      adoconnection1.Connected := true;
      if adoconnection1.Connected then    //判断是否连接成功
      begin
        application.MessageBox('连接成功!','提示');
        adoquery1.SQL.Add('select * from rc');
        edit1.Text:=IntToStr(ADOQuery1.RecordCount);
        adoconnection1.Connected := false; //关闭
      end;
      except
        application.MessageBox('数据库连接失败,请确认你已配置好服务器','提示');
        frmserver:=TfrmServer.Create(Application);
        frmserver.ShowModal;
      end;
     end;end.
    结果还是出错:首先提示连接成功,然后跳出编译错误:
    标题:Debugger Exception Notification
    内容: Project Project1.exe raised exception class EDatabaseError with message'ADOQuery1:Cannot perform this operation on a closed dataset',Process stopped,Use Step or Run to continue.
    问题是不是出在别处.
      

  6.   

    我如果故意在配置文件中写入错误的登录用户和密码,它不是跳出提示和配置界面,而是出现编译错误(Project Project1.exe raised exception class EOLeExxception with message'用户名xx登录失败.'Process stopped,Use Step or Run to continue.).我再编译运行后才会出现提示和配置界面的.
      

  7.   

    adoquery都没有open,怎么会有recordcount?
    如果想要记录总数的话直接count(*)就可以,idh应该是唯一主键嘛
      

  8.   

    目前代码如下:
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, DB, ADODB, StdCtrls, Menus,IniFiles;type
      TForm1 = class(TForm)
        ADOConnection1: TADOConnection;
        Button1: TButton;
        Label1: TLabel;
        MainMenu1: TMainMenu;
        N1: TMenuItem;
        X1: TMenuItem;
        O1: TMenuItem;
        S1: TMenuItem;
        N2: TMenuItem;
        Label2: TLabel;
        Edit1: TEdit;
        Label3: TLabel;
        Edit2: TEdit;
        Label4: TLabel;
        Edit3: TEdit;
        ADOQuery1: TADOQuery;
        procedure X1Click(Sender: TObject);
        procedure S1Click(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      myinifile:Tinifile;implementationuses Unit3;{$R *.dfm}procedure TForm1.X1Click(Sender: TObject);
    begin
      adoconnection1.Close;
      close;
    end;procedure TForm1.S1Click(Sender: TObject);
    begin
     frmserver.ShowModal;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
     server:string;
     database:string;
     password:string;
     user:string;
    begin
     try   //判断连接的语句,首先读取配置文件-待修改
      myinifile:=Tinifile.Create(ExtractFilePath(paramstr(0))+'setup.ini');
      password:=myinifile.readstring('setup','user_pwd','');
      user:=myinifile.readstring('setup','user_name','');
      database:=myinifile.readstring('setup','srv_data','');
      server:=myinifile.readstring('setup','data_source','');
      adoconnection1.Close;
      adoconnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='
      +password+';Persist Security Info=True;User ID='
      +user+';Initial Catalog='+database
      +';Data Source='+server;
      adoconnection1.Connected := true;
      if adoconnection1.Connected then    //判断是否连接成功
      begin
        application.MessageBox('连接成功!','提示');
        adoquery1.Open;
        edit1.Text:=IntToStr(select idh from rc);
        edit2.Text := (SELECT T_Eight FROM date WHERE (Date = LEFT(GETDATE(), 10)));
        adoconnection1.Connected := false; //关闭
      end;
      except
        application.MessageBox('数据库连接失败,请确认你已配置好服务器','提示');
        frmserver:=TfrmServer.Create(Application);
        frmserver.ShowModal;
      end;
     end;
      

  9.   

    是不是少了这句
    adoquery1.connection:=adoconnection1;
      

  10.   

    ADOQUERY 也没有OPEN  也没有指定连接的ADOCONTENT  好晕