【问题标题】:how i fill 2 comboboxs from 2 different tables?我如何从 2 个不同的表中填充 2 个组合框?
【发布时间】:2016-02-10 09:14:25
【问题描述】:

我需要从 2 个不同的表中填充 2 个组合框。这种方式行不通。

private void client_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=.;Initial Catalog=SCP_DB;Integrated Security=True";
    con.Open();
    string query = "select  Operation_Type from Operations_Types";
    SqlDataAdapter da = new SqlDataAdapter(query, con);
    DataSet ds = new DataSet();
    da.Fill(ds, "Operations_Types");
    comboOpType.DisplayMember = "Operation_Type";
    comboOpType.ValueMember = "Operation_Type";
    comboOpType.DataSource = ds.Tables["Operations_Types"];

    da.Fill(ds, "Payment_Type");
    comboPayType.DisplayMember = "Payment_Types";
    comboPayType.ValueMember = "Payment_Types";
    comboPayType.DataSource = ds.Tables["Payment_Type"];
    SqlCommand cmd = new SqlCommand(query, con);
    cmd.ExecuteNonQuery();
    con.Close();
}

【问题讨论】:

  • 你能展示一下你的桌子是什么样子的吗?
  • 你说的不起作用是什么意思?您收到任何异常或错误消息?出乎意料的结果?您能否更具体地说明您的问题?
  • 现在有异常
  • 第一个 como 正在工作
  • @AlyRamadan 我建议您在问题中澄清“不起作用”的含义。

标签: c# sql ado.net


【解决方案1】:

您应该调用您的 sql 实例中的每个表并使用 TableMappings

像这样更改您的代码。

private void client_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=.;Initial Catalog=SCP_DB;Integrated Security=True";
    con.Open();
    string query = "select  Operation_Type from Operations_Types; select ? from Payment_Type";
    SqlDataAdapter da = new SqlDataAdapter(query, con);
    da.TableMappings.Add("Table", "Operations_Types");
    da.TableMappings.Add("Table1", "Payment_Type");
    DataSet ds = new DataSet();
    da.Fill(ds, "Table");
    comboOpType.DisplayMember = "Operation_Type";
    comboOpType.ValueMember = "Operation_Type";
    comboOpType.DataSource = ds.Tables["Operations_Types"];

    da.Fill(ds, "Table1");
    comboPayType.DisplayMember = "Payment_Types";
    comboPayType.ValueMember = "Payment_Types";
    comboPayType.DataSource = ds.Tables["Payment_Type"];
    SqlCommand cmd = new SqlCommand(query, con);
    cmd.ExecuteNonQuery();
    con.Close();
}

【讨论】:

  • 很高兴为您提供帮助。谢谢。
猜你喜欢
  • 2020-06-17
  • 1970-01-01
  • 2018-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多