Getting values back from OleDbDataReader reading from Access database

后端 未结 2 1276
不思量自难忘°
不思量自难忘° 2020-12-21 02:25

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

2条回答
  •  难免孤独
    2020-12-21 02:42

    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\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");
       }
    

提交回复
热议问题