Odzyskiwanie wartości z odczytu OleDbDataReader z bazy danych Access
Poniżej kod, którego używam, aby połączyć się z bazą danych Access i pobrać wartości z zapytania. Problem polega na tym, że nie mogę odzyskać żadnych wartości z obiektu czytnika. Widzę, że istnieje poprawna ilość wierszy, jednak wciąż otrzymuję wyjątek InvalidOperationException (czy używam GetValue () lub GetString ()) mówiąc „Brak danych dla wiersza / kolumny”.
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();
}