C# 使用数据库中的数据绑定组合框
我有windows窗体,我添加了combobox,其中包含数据库中的bin数据,我添加了代码,但出现了此错误(无效列名类别),尽管名称正确C# 使用数据库中的数据绑定组合框,c#,winforms,C#,Winforms,我有windows窗体,我添加了combobox,其中包含数据库中的bin数据,我添加了代码,但出现了此错误(无效列名类别),尽管名称正确 public Category() { InitializeComponent(); CategoryParent(); } private void CategoryParent() { using (SqlConnection Con = GetConnection()) { SqlDataAdapter
public Category()
{
InitializeComponent();
CategoryParent();
}
private void CategoryParent()
{
using (SqlConnection Con = GetConnection())
{
SqlDataAdapter da = new SqlDataAdapter("Select Category.Category,Category.Id from Category", Con);
DataTable dt = new DataTable();
da.Fill(dt);
CBParent.DataSource = dt;
CBParent.DisplayMember = "Category";
CBParent.ValueMember = "Id";
}
}
像这样更改您的查询
Select Category.Category as CatName ,Category.Id from Category
CBParent.DisplayMember = "CatName";
i-e为您的列使用类似“CatName”的别名,并按如下方式设置显示成员
Select Category.Category as CatName ,Category.Id from Category
CBParent.DisplayMember = "CatName";
希望能有所帮助。我检查了连接,发现连接字符串错误在首选SQL工具中执行SQL
选择Category.Category,Category.Id from Category
时会发生什么?就我个人而言,我认为问题出在查询上——可能是因为列名的大小写敏感,或者输入错误。当您运行这是Managemenet Studio时,结果中的列名是什么?它们是否与代码中的相同?