我用第三方控件cxDBComboBox,用ado连接Access数据库,当点击cxDBComboBox时,下拉列表中的Item清除,然后重新从一个数据表中选择该列的值添加到cxDBComboBox的下拉列表中,用代码怎么写呀?我是初学者,折腾两天了快要放弃了,才到此胜地求救!大师们可否提供完全的代码,先谢了!
顺便问一下,ado连接数据库的信息放在一个文件中,无论该程序移植到那都不用该connectionstring 就能运行,这个文件怎么写呀?

解决方案 »

  1.   

    procedure TForm2.FormCreate(Sender: TObject);
    begin
      with datamodule1.TDF_Manu do
        begin
          first;
          while not eof do
            begin
              DBComboBox7.Items.Add(fieldbyname('manu').AsString);
              next;
            end;
        end;
      

  2.   

    ado连接数据库的信息可以放到ini文件然后读取例子
    ini 文件connect.ini
    [connect]
    Server=Server01
    DB=YLJZ
    User=YLJZ
    Ps=YLJZ连接
    var
      ConnectIni:TIniFile;
      FileName:String;
      servername,DB,User,Ps:string;
    begin
      FileName:='c:\connect.ini';
      ConnectIni:=TIniFile.Create(FileName);
      ServerName:=ConnectIni.ReadString('connect','server','null');
      DB:=ConnectIni.ReadString('connect','DB','null');
      User:=ConnectIni.ReadString('connect','User','null');
      Ps:=ConnectIni.ReadString('connect','Ps','null');
      AdoConnection1.ConnectionString:='Provider=SQLOLEDB.1;Password='+ps+';Persist Security Info=True;';
      AdoConnection1.ConnectionString:=AdoConnection1.ConnectionString+'User ID='+User+';Initial Catalog='+DB+';Data Source='+ServerName+'';
      try
        AdoConnection1.Connected:=True;
      except
        showmessage('请检查数据库连接');
      end;
      

  3.   

    在form上放一个adoquery,设置她的adoconnection 然后
    在cxDBComboBox 的onEnter时间里加入如下代码
    with adoquery1 do
    begin
     close;
     sql.text := 'select Item from aTable';
     open;
      cxDBComboBox.items.clear;
      while not eof do
      begin
       cxDBComboBox.items.add(fields[0].asstring);
       next;
      end;
     close;
    end;关于第二个问题你可以将这个字符串存到一个ini文件中,每次数据模块创建时从改文件中读取该串,然后将adoconnection的connectionString设成这个字符串即可
      

  4.   

    我用的cxDBComboBox空间怎么没有Items.add()这个属性亚?
      

  5.   

    wangjintu(wangjin) :
    怎么编译出错提示:[Error] Unit2.pas(139): Undeclared identifier: 'items'
      

  6.   

    我用第三方控件cxDBComboBox,用ado连接Access数据库,当点击cxDBComboBox时,下拉列表中的Item清除,然后重新从一个数据表中选择该列的值添加到cxDBComboBox的下拉列表中,用代码怎么写呀?我是初学者,折腾两天了快要放弃了,才到此胜地求救!大师们可否提供完全的代码,先谢了
    --------------------------
    procedure TForm1.cxDBComboBox1DropDown(Sender: TObject);
    begin
      with cxDBComboBox1.Items do
        try
          BeginUpdate;
          Clear;
          //添加代码部分就用楼上的吧
        finally
          EndUpdate;
        end;
    end;
      

  7.   

    不会吧,这是什么combobox控件,竟然连这个都没有,建议使用rx控件替代
      

  8.   

    同意楼上的,没有必要为了一些无关紧要的东西去用别的控件,一般情况下,用delphi自带的就够了
      

  9.   

    我用自带的空间,但当运行时选择数据后,combobox失去焦点后combobox的数据就又没了,
    这是怎么回事呀?
      

  10.   

    你用的是dbcombobox吧,没有指定数据源和数据字段,当然一失去焦点数据就没了,因为控件不知道数据要存到哪里去
      

  11.   

    在DropDown事件中使用一下代码试试:procedure TForm1.ComboBox1DropDown(Sender: TObject);
    begin
      combobox1.Clear;
      adoquery1.First;
      while not adoquery1.Eof do
          begin
              combobox1.Items.Add(adoquery1.FieldByName('Id').AsString);
              adoquery1.Next;
          end;
    end;
      

  12.   

    with Adoquery do
    begin
      close;
      sql.clear;
      sql.add(' select * from table_name');
      
      try
        open;
        cxDBComboBox.Properties.Items.Clear;
        while not eof do
        begin
          cxDBComboBox.Properties.Items.add(Fieldbyname('字段名').value);
          next;
        end;
      except
      end;
    end;
      

  13.   

    with ADOQuery1 do
       begin
         close;
           sql.Clear;
           sql.text := 'Insert Into tbCarriage (CarriageNo,Memo)  Values(:CarriageNo,:Memo)';
           Parameters.ParamByName('CarriageNo').Value := ComboBox1.Text;
           Parameters.ParamByName('Memo').Value := Memo1.Text;
           ExecSQL;
           close;
       end;
    怎么运行时老是出错,要是把改成如下就不会出错,而求数据库(access)中Memo字段允许为空
    with ADOQuery1 do
       begin
         close;
           sql.Clear;
           sql.text := 'Insert Into tbCarriage (CarriageNo)  Values(:CarriageNo)';
           Parameters.ParamByName('CarriageNo').Value := ComboBox1.Text;
           Parameters.ParamByName('Memo').Value := Memo1.Text;
           ExecSQL;
           close;
       end;
    我用的是自带的Memo控件,为什么用Memo就会出错说是Inser Into语句语法错误,去了他就没错,这是怎么回事呀?
      

  14.   

    with ADOQuery1 do
       begin
         close;
           sql.Clear;
           sql.text := 'Insert Into tbCarriage (CarriageNo,Memo)  Values(:CarriageNo,:Memo)';
           Parameters.ParamByName('CarriageNo').Value := ComboBox1.Text;
           Parameters.ParamByName('Memo').Value := Memo1.Text;
           ExecSQL;
           close;
       end;
    怎么运行时老是出错,要是把改成如下就不会出错,而求数据库(access)中Memo字段允许为空
    with ADOQuery1 do
       begin
         close;
           sql.Clear;
           sql.text := 'Insert Into tbCarriage (CarriageNo)  Values(:CarriageNo)';
           Parameters.ParamByName('CarriageNo').Value := ComboBox1.Text;
           ExecSQL;
           close;
       end;
    我用的是自带的Memo控件,为什么用Memo就会出错说是Inser Into语句语法错误,去了他就没错,这是怎么回事呀?
      

  15.   

    Memo可能是系统保留字,改个名
      

  16.   

    combobox的text类型和memo的text类型不一样。
    数据库里面的字段是什么类型??
      

  17.   

    combobox的text类型和memo的text类型
    数据库是文本的
      

  18.   

    with ADOQuery1 do
       begin
         close;
           sql.Clear;
           sql.text := 'Insert Into tbCarriage (CarriageNo,Memo)  Values(:CarriageNo,:Memo)';
           Parameters.ParamByName('CarriageNo').Value := ComboBox1.Text;
           Parameters.ParamByName('Memo').Value := Memo1.Text;
           ExecSQL;
           close;
       end;
    怎么运行时老是出错,要是把改成如下就不会出错,而求数据库(access)中Memo字段允许为空
    with ADOQuery1 do
       begin
         close;
           sql.Clear;
           sql.text := 'Insert Into tbCarriage (CarriageNo)  Values(:CarriageNo)';
           Parameters.ParamByName('CarriageNo').Value := ComboBox1.Text;
           ExecSQL;
           close;
       end;
    我用的是自带的Memo控件,为什么用Memo就会出错说是Inser Into语句语法错误,去了他就没错,这是怎么回事呀?
    combobox的text类型和memo的text类型
    数据库是文本的