Select specific column of file . MDB in C#

Asked

Viewed 54 times

-1

I need to use data from a specific column in a file .MDB. I can read the file, but it only shows me first column, I tried to change the SELECT, changing the * by column name, but it didn’t work out

Reference: code of the http://www.macoratti.net/Default.aspx

using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AcessoMDB
{
    class Program
    {
        static void Main(string[] args)
        {

                    //cria a conexão com o banco de dados
            OleDbConnection aConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\c066408\Desktop\Sprint\68\16417\BEC.MZ.BEX0.R007T.D0220.P0724.AL\BEC.MZ.BEX0.R007T.D0220.P0724.AL.mdb");

           //cria o objeto command and armazena a consulta SQL
            OleDbCommand aCommand = new OleDbCommand("select * from SysTable", aConnection);

           try
              {
                aConnection.Open();
                //cria o objeto datareader para fazer a conexao com a tabela
                OleDbDataReader aReader = aCommand.ExecuteReader();

                Console.WriteLine("Os valores retornados da tabela são : ");

                //Faz a interação com o banco de dados lendo os dados da tabela
                while(aReader.Read())
                {
                    Console.WriteLine(aReader.GetString(1));
                }
                //fecha o reader
                aReader.Close();
                //fecha a conexao
               aConnection.Close();
                //pausa
                Console.ReadLine();
             }
            //Trata a exceção
            catch(OleDbException e)
            {
                Console.WriteLine("Error: {0}", e.Errors[0].Message);
            }


        }
    }
}
  • What didn’t work out?

1 answer

1


Strictly looking at your code. You are consulting all the columns but writing only one.

In your while it says display the other columns, respect the return types of your query, where string use string where numeric use Int.

//Faz a interação com o banco de dados lendo os dados da tabela
  while(aReader.Read())
  {
       Console.WriteLine(aReader.GetString(0));
       Console.WriteLine(aReader.GetString(1));
       Console.WriteLine(aReader.GetString(2));
       Console.WriteLine(aReader.GetInt32(3));
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.