コード例 #1
0
 private void setCombo(string sql, CheckedComboBoxWithLable cmb)
 {
     //清空下拉列表
     cmb.ClearItem();
     //cmb.Items.Add("%");
     //cmb.Text = "%";
     //实例化一个类
     getLxerpConn v = new getLxerpConn();
     //获取类里面的sql连接到数据库的字符串
     v.getsqlconn();
     //  string connStr = "Data Source=.\\sql2008;Initial Catalog=books;Integrated Security=True"; //连接到数据库的字符串
     SqlConnection cn = new SqlConnection(v.Sqlconn);
     //打开链接
     cn.Open();
     SqlCommand cmd = cn.CreateCommand();
     cmd.CommandText = sql;
     SqlDataReader dr = cmd.ExecuteReader();
     while (dr.Read())
     {
         cmb.Add_CB_Item(dr.GetValue(0).ToString().Trim());
     }
     cn.Close();
     cn.Dispose();
 }