我有一個下拉列表ComboBox,現想將數據庫中的User_Password的用戶名User_Name字段用代碼動態加載到此列表框,請指教,謝謝!

解决方案 »

  1.   

    function GetFieldValuesList(ATableName: string;
      AFieldName: string; AStringList: TStrings): boolean;
    var
      str: string;
      QryTmp: TADOQuery;
    begin
      Result := True;
      QryTmp := TADOQuery.Create(nil);
      with QryTmp do
      try
        Connection := DM.ADOC;
        if Active then Active := False;
        SQL.Text := 'Select Distinct ' +
          AFieldName + ' From ' + ATableName;
        Open;
        First;
        AStringList.Clear;
        while not Eof do
        begin
          str := Trim(Fields[0].AsString);
          if Str <> '' then AStringList.Add(str);
          Next;
        end;
        Close;
      except
        Result := False;
      end;
    end;
      

  2.   

    procedure TForm1.FormActivate(Sender: TObject);
    begin
     Try
     If not Adotable1.Active  then
       Adotable1.Active := true
       Adotable1.First;
       while not Adotable1.Eof do
       begin
       combobox1.Items.Add(Adotable1.fieldbyname().AsString);
       Adotable1.Next;
       end;
     except
     showmessage('无法加在数据');
     end;
    end;
      

  3.   

    ComboBox1.Items.Add(username+password);
      

  4.   

    从数据库中查出数据然后依次
    combobox1.items.add('用户名');
      

  5.   

    在你的窗体显示事件中加如下代码with ADOQuery1 do
    begin
      sql.clear;
      sql.add('select User_Name from table');
      //这里不要加密码吧?table代表你存放用户信息的表
      open;
      while not eof do
      begin
        ComboBox1.Items.Add(FieldByName('User_Name').AsString);
        Next;
      end;
    end;