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");
}
}
OleDbDataReader.GetString将int
作为参数,并返回string
值。
// 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.
您可能想要更像的东西
string b = dr.GetString(dr.GetOrdinal("book"));
由于GetString()
方法(与GetInt32
和所有其他方法一样)采用列索引,可以使用GetOrdinal
方法返回