我在 reader.GetString
遇到编译时错误,知道为什么吗?
代码:
using (var connection = new OleDbConnection())
{
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Sparrow vivek\Documents\Billing.accdb";
connection.Open();
var query = "SELECT ItemCode FROM invoice";
using (var command = new OleDbCommand(query, connection))
{
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
comboBox1.Items.Add(reader.GetString("ItemCode"));
comboBox2.Items.Add(reader.GetString("ItemCode"));
}
}
}
}
最佳答案
OleDbDataReader.GetString
method将 int
作为参数,而不是 string
。
public override string GetString(
int i
)
它采用从零开始的列号。
由于您只有一列,因此将其更改为;
while (reader.Read())
{
comboBox1.Items.Add(reader.GetString(0));
comboBox2.Items.Add(reader.GetString(0));
}
关于c# - 将字符串转换为 int 时出现 Datareader 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27486685/