我正在创建一种表格,必须将数据库中的数据显示到组合框中,需要我的帮助
我已经尝试下载MySql Server,但是,它最多仅支持Visual Studio 2017,并且我具有Visual Studio 2019
using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Code\C#\MyFirstUI\MyFirstUI\LOGIN.mdf;Integrated Security=True"))
{
try
{
string query = "select USERNAME from LOGIN";
SqlDataAdapter da = new SqlDataAdapter(query, conn);
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds, "Username");
comboBox3.DisplayMember = "Userame";
comboBox3.DataSource = ds.Tables["Username"];
}
catch (Exception ex)
{
// write exception info to log or anything else
MessageBox.Show(ex.Message,"Error occured!");
}
}
我本来希望从数据库中获得数据,但是我什么也没得到
答案 0 :(得分:0)
comboBox3.ValueMember="USERNAME"
和comboBox3.DisplayMember="USERNAME"
,使用“ USERNAME”而不是“ Userame”,因为您的SQL是select USERNAME
ds.Tables["Username"].Rows.Count>0