C# 如何从oracle数据库填充组合框?

C# 如何从oracle数据库填充组合框?,c#,oracle,combobox,oracle11g,C#,Oracle,Combobox,Oracle11g,我正在尝试从oracle数据库填充组合框,然后根据 从组合框中选择的值我想自动填充textbox1,textbox2和textbox3 下面是我试图用来填充组合框的代码 第一个问题是组合框加载为空 其次,我想学习如何自动填充其他textbox 如果有人能帮我或向我解释,请告诉我 public void Fillcombobox() { string connstr = "data source=db;user id=user;password=pwd;";

我正在尝试从oracle数据库填充
组合框
,然后根据 从
组合框中选择的值
我想自动填充
textbox1
textbox2
textbox3

下面是我试图用来填充组合框的代码 第一个问题是
组合框
加载为空 其次,我想学习如何自动填充其他
textbox

如果有人能帮我或向我解释,请告诉我

    public void Fillcombobox()
    {
        string connstr = "data source=db;user id=user;password=pwd;";
        string cmdtxt = "select product_id, description from products";

        using (OracleConnection conn = new OracleConnection(connstr))
        using (OracleCommand cmd = new OracleCommand(cmdtxt,conn))
        {
            conn.Open();
            OracleDataReader dr;
            cmd.ExecuteReader();
            CB_PRODUCT_ID.DisplayMember = "product_id";
            CB_PRODUCT_ID.ValueMember = "description";

            while (dr.Read())
            {
                // what should I d to fill other textboxs ?
                TB_PRODUCTS.Text = ???;
            }
        }

    }

    private void CB_PRODUCT_ID_SelectedIndexChanged(object sender, EventArgs e)
    {
        Fillcombobox();
    }
您必须通过组合光标中的所有值,例如,
列表


您应该合并读取器中的值,但不能重写。虽然此代码可能会回答问题,但提供了有关此代码回答问题的原因和/或如何提高其长期价值的附加上下文。@ryanyyu您是对的,主要问题是如何更改textbox1、textbox2、,textbox3根据combobox的值?我已经将其绑定,但是
combobox
的所有值都在一行中,我试图替换sb.Append(Convert.ToString(dr[“YourCoumnName”]);与某人一起追加(转换为字符串(dr[“YourCoumnName”));但这是习惯help@samer:很抱歉,您似乎必须将项目与
列表
组合,而不是与
StringBuilder
组合。我已编辑了答案。出现两个错误1-与“System.Windows.Forms.ComboBox.ObjectCollection.AddRange(object[])”匹配的最佳重载方法有一些无效参数2-参数1:无法在TB_PRODUCTS.Items.AddRange(Items)行从“System.Collections.Generic.List”转换为“object[]”;你知道TB_产品是一个组合框,对吗?
  while (dr.Read())
            {
                TB_PRODUCTS.Text = dr["YourColumnName"].toString();
            }
    public void Fillcombobox()
    {
        //TODO: do not hardcode connection string (esp. password), but load it
        string connstr = "data source=db;user id=user;password=pwd;";

        string cmdtxt = 
          @"select product_id, 
                   description 
              from products";

        using (OracleConnection conn = new OracleConnection(connstr))
        using (OracleCommand cmd = new OracleCommand(cmdtxt, conn))
        {
            conn.Open();

            // reader is IDisposable and should be closed
            using (OracleDataReader dr = cmd.ExecuteReader()) 
            {
                List<String> items = new List<String>();

                while (dr.Read())
                {
                    items.Add(String.Format("{0}, {1}", dr.GetValue(0), dr.GetValue(1)));
                } 

                TB_PRODUCTS.Items.AddRange(items.ToArray());
            }

        }

    }
private void TB_PRODUCTS_TextChanged(object sender, EventArgs e) {
  ComboBox cb = sender as ComboBox;

  // Either explicitly or via cb.SelectedIndex
  textbox1.Text = cb.Text;
}