[英]Getting values back from OleDbDataReader reading from Access database
Below it the code I'm using to connect to an Access database and pull the values from the query. 下面是我用来连接Access数据库并从查询中提取值的代码。 Problem is.. I cannot get any values back from the reader object.
问题是..我无法从读者对象中获取任何值。 I can see that there are the correct amount of rows, however I keep getting an InvalidOperationException (whether I use GetValue() or GetString()) saying "No data exists for the row/column."
我可以看到有正确的行数,但是我不断得到InvalidOperationException(我是否使用GetValue()或GetString())说“行/列没有数据”。
System.Data.OleDb.OleDbConnection conn = new
System.Data.OleDb.OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft Office 12.0 Access Database Engine OLE DB Provider;" +
@"Data source= C:\Users\nearod\Desktop\ImportDB.accdb";
try
{
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [SQL Agent Unique ID Test Load]", conn);
OleDbDataReader reader = cmd.ExecuteReader();
string companyCode = reader.GetValue(0).ToString();
string agentId = reader.GetString(1);
string firstName = reader.GetString(2);
string lastName = reader.GetString(3);
string nameSuffix = reader.GetString(4);
string corporateName = reader.GetString(5);
string entityType = reader.GetString(6);
string obfSSN = reader.GetString(7);
string obfFEIN = reader.GetString(8);
string dummyIndicator = reader.GetString(9);
// Insert code to process data.
}
catch (Exception ex)
{
MessageBox.Show("Failed to connect to data source");
}
finally
{
conn.Close();
}
you have to call Read method like below (use using instead of disposing yourself connection 你必须像下面这样调用Read方法(使用而不是处理你自己的连接
string connectionString = @"Provider=Microsoft Office 12.0 Access Database Engine OLE DB Provider;" + @"Data source= C:\Users\nearod\Desktop\ImportDB.accdb";
string queryString= "SELECT * FROM [SQL Agent Unique ID Test Load]";
try
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string companyCode = reader.GetValue(0).ToString();
string agentId = reader.GetString(1);
string firstName = reader.GetString(2);
string lastName = reader.GetString(3);
string nameSuffix = reader.GetString(4);
string corporateName = reader.GetString(5);
string entityType = reader.GetString(6);
string obfSSN = reader.GetString(7);
string obfFEIN = reader.GetString(8);
string dummyIndicator = reader.GetString(9);
// Insert code to process data.
}
reader.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("Failed to connect to data source");
}
Modify your code like this: 像这样修改你的代码:
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
string companyCode = reader.GetValue(0).ToString();
string agentId = reader.GetString(1);
string firstName = reader.GetString(2);
string lastName = reader.GetString(3);
string nameSuffix = reader.GetString(4);
string corporateName = reader.GetString(5);
string entityType = reader.GetString(6);
string obfSSN = reader.GetString(7);
string obfFEIN = reader.GetString(8);
string dummyIndicator = reader.GetString(9);
// Insert code to process data.
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.