繁体   English   中英

如何将数据库中的数据同步到组合框

[英]How do I sync data from a database to a combobox

我正在创建一种形式,我必须将数据库中的数据显示到组合框中,我需要帮助才能完成它,请

我已经尝试下载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!");
    }
}

我本来期望数据库中的数据然而,我没有输出

  1. 设置comboBox3.ValueMember="USERNAME"comboBox3.DisplayMember="USERNAME" ,使用“USERNAME”,而不是“Userame”,因为你的sql是select USERNAME
  2. 检查是否ds.Tables["Username"].Rows.Count>0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM