Fill Combobox from database

后端 未结 7 1186
北海茫月
北海茫月 2020-11-28 12:22

I have an error with a combobox

My code:

SqlConnection conn = new SqlConnection();
try
{
    conn = new SqlConnection(@\"Data Source=SHARKAWY;Initia         


        
相关标签:
7条回答
  • 2020-11-28 13:07
                SqlConnection conn = new SqlConnection(@"Data Source=TOM-PC\sqlexpress;Initial Catalog=Northwind;User ID=sa;Password=xyz") ;
                conn.Open();
                SqlCommand sc = new SqlCommand("select customerid,contactname from customers", conn);
                SqlDataReader reader;
    
                reader = sc.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Columns.Add("customerid", typeof(string));
                dt.Columns.Add("contactname", typeof(string));
                dt.Load(reader);
    
                comboBox1.ValueMember = "customerid";
                comboBox1.DisplayMember = "contactname";
                comboBox1.DataSource = dt;
    
                conn.Close();
    
    0 讨论(0)
提交回复
热议问题