【发布时间】:2019-12-16 21:34:31
【问题描述】:
我已尝试显示,但没有任何结果。
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection("DataSource=10.6.9.58;Initial Catalog=DBNew;User ID=qwertyuiop;Password=1234567890"))
{
try
{
string query = "select ID,HRName from HRTable";
SqlDataAdapter da = new SqlDataAdapter(query, conn);
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds, "HRTable");
comboBox1.DisplayMember = "HRName";
comboBox1.ValueMember = "ID";
comboBox1.DataSource = ds.Tables["HRTable"];
}
catch (Exception ex)
{
// write exception info to log or anything else
MessageBox.Show("Error occured!");
}
}
ComboBox 不显示任何值。 是数据库连接有问题还是代码有问题?
【问题讨论】:
-
欢迎来到 SO。我建议先阅读 the tour,然后阅读 How to Ask 和 Minimal, Complete, and Verifiable example,了解 SO 的工作原理以及如何有效地提出问题。
-
用“C# mvc dropdown data bind”点击搜索引擎,随机选择答案:stackoverflow.com/questions/22829464/…
标签: c# .net sql-server model-view-controller