2014-09-25 15 views
-4
void Fillcombo() 
    { 
     OleDbConnection cn = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source=Library.accdb"); 
     OleDbCommand cmd = new OleDbCommand(@"SELECT * FROM Books", cn); 
     cmd.Connection = cn; 

     OleDbDataReader dr; 
     try 
     { 
      cn.Open(); 
      dr = cmd.ExecuteReader(); 
      while (dr.Read()) 
      { 
       string b = dr.GetString("book");//This is the line where im getting an error 
       cboProgramming.Items.Add(b); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("f"); 
     } 
    } 
+0

欢迎返回到计算器,请阅读:https://stackoverflow.com/help/how-to-ask – flayn 2014-09-25 08:13:22

回答

1
// Summary: 
    //  Gets the value of the specified column as a string. 
    // 
    // Parameters: 
    // ordinal: 
    //  The zero-based column ordinal. 
    // 
    // Returns: 
    //  The value of the specified column. 
    // 
    // Exceptions: 
    // System.InvalidCastException: 
    //  The specified cast is not valid. 
    public override string GetString(int ordinal); 

你应该传递一个int。

1

你可能想更多的东西一样:

string b = dr.GetString(dr.GetOrdinal("book")); 

由于GetString()方法(如GetInt32和所有其他人)都将列指数 - 该指数可以与GetOrdinal方法

+0

+1这是最完整的答案。 – DGibbs 2014-09-25 08:21:59