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";



                }

  

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

上一篇:数据库查询优化的12种方式


下一篇:调度器18—/proc/schedstat文件