C# 将数据从数据库获取到组合框
短的 一个名为ComDet的数据库,以及列cIDPK、cName、cDet、mainCateFK、SubcatFK 这是为了将数据从表ComDet获取到组合框C# 将数据从数据库获取到组合框,c#,sql,combobox,C#,Sql,Combobox,短的 一个名为ComDet的数据库,以及列cIDPK、cName、cDet、mainCateFK、SubcatFK 这是为了将数据从表ComDet获取到组合框 DataSet ds2; private void searchBtn_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=.\SQLEXPRE
DataSet ds2;
private void searchBtn_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daSearch = new SqlDataAdapter("SELECT cName FROM ComDet", conn);
ds2 = new DataSet();
daSearch.Fill(ds2, "daSearch");
ListU.ValueMember = "cName";
ListU.DataSource = ds2.Tables["ComDet"];
ListU.DropDownStyle = ComboBoxStyle.DropDownList;
ListU.Enabled = true;
}
但是它不起作用。。我哪里出错了?
组合框中未显示数据库表ComDet中的DatacName 问题:您正在将数据库表名ComDet指定为数据源,而不是将数据表名DASAARCH指定给ComboBox 解决方案:您需要将有效的DataTable名称指定给ComboBox作为数据源 替换此项:
ListU.DataSource = ds2.Tables["ComDet"];
为此:
ListU.DataSource = ds2.Tables["daSearch"];
或
完整代码:
因此,问题是如何将数据从数据库获取到combobox。就我个人而言,我发现使用DataSet类是不可预测的——它很容易出错,就像询问者在这里遇到的那样 试试这种方法。它将所有cName读入一个列表,并将该列表绑定到组合框。简单易读的代码。使用using语句还可以确保有效地释放非托管资源
private void searchBtn_Click(object sender, EventArgs e)
{
var list = new List<string>();
using (var conn = new SqlConnection())
{
conn.ConnectionString =
"Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
using (var cmd = new SqlCommand("SELECT cName FROM ComDet", conn))
{
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
list.Add(Convert.ToString(reader["cName"]));
}
}
}
}
ListU.DataSource = new BindingSource(list, null);
ListU.DropDownStyle = ComboBoxStyle.DropDownList;
ListU.Enabled = true;
}
你没有告诉我们它不起作用意味着什么!我已经编辑了你的标题。请看,如果一致意见为“否”,则不应该。组合框中不显示数据库表ComDet中的DatacName。它是否在DataTable中?你有没有用调试器设置断点来查看返回了什么数据?啊。。我该怎么做?我对C@MohdNasrulIwanFajaruddin相当陌生:您需要向DataSource提供DataTable名称dasaarch,请参阅我编辑的答案,它可以工作perfectly@MohdNasrulIwanFajaruddin:不客气:,我很高兴能为你提供帮助。那么,如果我想从组合框中选择任何东西,我该怎么办。。显示数据库表中的所有内容。。例如,如果我从组合框中选择一个公司名称,然后,该按钮将所选公司名称的所有数据发送到datagridview..@MohdNasrulIwanFajaruddin:您可以获取组合框的SelectedItem属性以获取所选公司名称,并从CompanyName=ListU.SelectedItem.ToString的表中启动SQL Select query->Select*。其他信息:多部分标识符无法绑定System.Data.DataRowView。
DataSet ds2;
private void searchBtn_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
SqlDataAdapter daSearch = new SqlDataAdapter("SELECT cName FROM ComDet", conn);
ds2 = new DataSet();
daSearch.Fill(ds2, "daSearch");
ListU.ValueMember = "cName";
ListU.DataSource = ds2.Tables["daSearch"];
ListU.DropDownStyle = ComboBoxStyle.DropDownList;
ListU.Enabled = true;
}
private void searchBtn_Click(object sender, EventArgs e)
{
var list = new List<string>();
using (var conn = new SqlConnection())
{
conn.ConnectionString =
"Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
conn.Open();
using (var cmd = new SqlCommand("SELECT cName FROM ComDet", conn))
{
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
list.Add(Convert.ToString(reader["cName"]));
}
}
}
}
ListU.DataSource = new BindingSource(list, null);
ListU.DropDownStyle = ComboBoxStyle.DropDownList;
ListU.Enabled = true;
}