C# 数据库查询表字段,写入到combox

 

string dbConStr = ConfigurationManager.ConnectionStrings["dbConStr"].ConnectionString;

            using (SqlConnection conn = new SqlConnection(dbConStr))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    conn.Open();
                    cmd.CommandText = "SELECT *  FROM UserInfo";// where UserName like '%a%'";
                    //  cmd.Parameters.AddWithValue()    
                    SqlDataReader reader = cmd.ExecuteReader();
                    reader.Read();
                    label1.Text = reader.GetValue(1).ToString();

                    List<string> list = new List<string>();
                    //     string a = reader.GetValue(0).ToString();

                    // label1.Text = a;
                    //  reader.FieldCount
                    MessageBox.Show(reader.FieldCount.ToString());

                    ArrayList list1 = new ArrayList();
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        // comboBox1.Items.Add(reader.GetName(i));
                        list.Add(reader.GetName(i));

                        // comboBox1.DisplayMember = "ABCD";
                        // label1.Text += reader.GetName(i);     
                    }

                    //list1.Add(new DictionaryEntry("szf", "身份证"));  
                    //list1.Add(new DictionaryEntry("username", "用户名")); 
                    //list1.Add(new DictionaryEntry("sfz", "身份Y证号")); 
                    //list1.Add(new DictionaryEntry("Tel", "电话号码"));

                    comboBox1.DataSource = list;
                    // comboBox1.ValueMember = "UserId";
                    comboBox1.DisplayMember = "UserId";



                }

  

posted @ 2020-12-30 22:36  Nanyingming  阅读(350)  评论(0编辑  收藏  举报