2012-08-10 129 views
0

我想在选择无线电选项后在组合框内的数据库中显示项目。当我尝试这个时候,组合框中没有显示任何内容。请帮忙使用数据填充组合框

private void chkDetailsButton_Click(object sender, EventArgs e) 
{ 
    if (radioButtonA.Checked) 
    { 
     OleDbConnection connect = db.dbConnect(); 

     try 
     { 
      connect.Open(); 
      MessageBox.Show("Opened"); 
      OleDbCommand command = new OleDbCommand(); 
      command.Connection = connect; 

      command.CommandText = "Select * from Categories"; 
      DataTable dt = new DataTable(); 

      for (int i = 0; i < dt.Rows.Count; i++) 
      { 
       cmbDisplay.Items.Add(dt.Rows[i]["SeatNo"]); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Exception in Database" + ex); 
     } 
     finally 
     { 
      connect.Close(); 
     } 
    } 
} 
+2

你尝试调试代码? – 2012-08-10 09:51:37

+0

是的,我做了,它连接到数据库,但不显示SeatNo列中的项目 – 2012-08-10 09:52:52

+0

这是WinForms还是ASP.Net?如果你可以连接,那dt.Rows.Count是什么?您似乎没有将任何数据放入数据表中! – dash 2012-08-10 09:53:48

回答

3

有缺少填充DataTable dt与数据,这是由您的SQL命令返回。

+0

我想从访问数据库中获取数据 – 2012-08-10 09:59:04

4

你try块应如下:

try 
    { 
     connect.Open(); 
     MessageBox.Show("Opened"); 
     OleDbCommand command = new OleDbCommand(); 
     command.Connection = connect; 

     command.CommandText = "Select * from Categories"; 
     DataTable dt = new DataTable(); 

     //Put some data in the datatable!! 
     using(OleDbDataReader reader = command.ExecuteReader()) 
     { 
      dt.Load(reader); 
     } 

     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      cmbDisplay.Items.Add(dt.Rows[i]["SeatNo"]); 
     } 
    } 

您需要填写数据的DataTable中!

你也可以考虑以下几点:

 using(OleDbDataReader reader = command.ExecuteReader()) 
     { 
      while(reader.Read()) 
      { 
       cmbDisplay.Items.Add(reader.GetValue(reader.GetOrdinal("SeatNo")); 
      } 
     } 

这样,你甚至不需要使用数据表;对于大量数据来说,这是一种更高效的方法。

顺便说一句,你不妨考虑使用Using

using(OleDbConnection connect = db.dbConnect()) 
    { 
     try 
     { 
      connect.Open(); 
      //MessageBox.Show("Opened"); 
      using(OleDbCommand command = new OleDbCommand()) 
      { 
       command.Connection = connect; 
       command.CommandText = "SELECT * FROM Categories"; 
       using(IDataReader reader = command.ExecuteReader()) 
       { 
        while(reader.Read()) 
        { 
          cmbDisplay.Items.Add(reader.GetValue(reader.GetOrdinal("SeatNo")); 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("An erorr occured:" + ex); 
     }   
    } 

这将确保您的连接,指挥和读者对象设置。如果您打算保留连接的一个实例,这是不合适的,但是当您的代码离开using语句时它将被关闭并处置。

+0

你是最好的男人。有效!。谢谢 – 2012-08-10 10:04:53

0

我不能相信这一点。 :D

你什么时候准备用数据从数据库填充DataTable? 没有什么在

之间

DataTable dt = new DataTable();

for (int i = 0; i < dt.Rows.Count; i++)

+0

当你在那里时,你可能会提到SQL甚至没有被执行! – 2012-08-10 09:57:20

1

使用此代码尝试 - 您的样品不要将您的表数据绑定,创建表的新实例英寸

$ private void chkDetailsButton_Click(object sender, EventArgs e) 
{ 
if (radioButtonA.Checked) 
     { 
      OleDbConnection connect = db.dbConnect(); 

      try 
      { 
       connect.Open(); 
       MessageBox.Show("Opened"); 
       OleDbCommand command = new OleDbCommand(); 
       command.Connection = connect; 

       command.CommandText = "Select * from Categories"; 
       OleDbDataReader myReader = command.ExecuteReader(); 
       while (myReader.Read()) 
       { 
        cmbDisplay.Items.Add(myReader["SeatNo"]); 
       } 

      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Exception in Database" + ex); 
      } 
      finally 
      { 
       connect.Close(); 
      } 
     } 
    } 
0

您可以填写与组合框数据表:

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True"); 
      SqlDataAdapter da = new SqlDataAdapter("select * from Books",con); 
      DataTable dt = new DataTable(); 
      da.Fill(dt); 
      comboBox1.DisplayMember = "Name"; 
      comboBox1.ValueMember = "ID"; 
      comboBox1.DataSource = dt; 
0

在数据集连接尝试之前,你不灌装数据这个

 if(radio.checked) 
    { 
     try 
     { 
     connect.Open(); 
     MessageBox.Show("Opened"); 
     OleDbCommand command = new OleDbCommand(); 
     command.Connection = connect; 
     command.CommandText = "Select * from Categories"; 

     OleDbDataAdapter db = new OleDbDataAdapter(); 
     DataSet ds = new DataSet(); 
     db.SelectCommand = command; 
     db.Fill(ds); 
     for(int i=0;i<ds.Tables[0].Rows.Count;i++) 
     { 
      cmbDisplay.Items.Add(ds.Tables[0].Rows[i][0].ToString());  

      } 
     cmDisplay.DataBind(); 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Exception in Database" + ex); 
     } 
     finally 
     { 
      connect.Close(); 
     } 
    }