Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

c# - 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. 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."

        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
earodDesktopImportDB.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();
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you have to call Read method like below (use using instead of disposing yourself connection

string connectionString = @"Provider=Microsoft Office 12.0 Access Database Engine OLE DB Provider;" + @"Data source= C:Users
earodDesktopImportDB.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");
   }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...