0

データベース内の特定の列のすべての値を持つコンボボックスを作成するにはどうすればよいですか。名前の付いた列がStudentNameあり、すべての値を含むコンボボックスが必要ですStudentName

sql = new SqlConnection(@"Data Source=PC-PC\PC;Initial Catalog=Anbar;Integrated Security=True");
adapter = new SqlDataAdapter("select * from School", sql);

どうすれば続行できますか?これらのコードを継続するためのいくつかのコードを教えてください。助けていただければ幸いです。

4

2 に答える 2

5
sqlCon = new SqlConnection(@"Data Source=PC-PC\PC;Initial Catalog=Anbar;Integrated Security=True");
  SqlDataAdapter da = new SqlDataAdapter("Select StudentName from School", sqlCon);
        DataTable dt = new DataTable();
        da.Fill(dt);
        yourComboBox.DataSource = dt;
        yourComboBox.DisplayMember = "StudentName";
        yourComboBox.ValueMember = "StudentName";

また、ComboBoxのデータベースからこのPopulateDateを読んでください

于 2012-05-03T07:11:35.430 に答える
1

次のコードを使用します

SqlDataAdapter da = new SqlDataAdapter("Select StudentName from School", sqlCon);
DataTable dat = new DataTable();
da.Fill(dat);
cmb.DataSource = dat;
cmb.DisplayMember = "StudentName";
cmb.ValueMember = "StudentName";
于 2012-05-03T07:21:41.803 に答える