//建立与数据库的连接 string strcon = ConfigurationManager.ConnectionStrings["XU"].ConnectionString; SqlConnection con = new SqlConnection(strcon); // 指定数据集 SqlCommand com = new SqlCommand(); com.CommandText = "select * from dbstudents "; com.Connection = con; //建表 DataTable dt = new DataTable(); //适配器 SqlDataAdapter sda = new SqlDataAdapter(); sda.SelectCommand = com; //充满table sda.Fill(dt); //foreach (DataRow row in dt.Rows) //{ // comboBox2.Items.Add(row[0].ToString()); //} comboBox2.DataSource = dt; comboBox2.DisplayMember = "name"; comboBox2.ValueMember = "age";
时间: 2024-11-08 23:05:52