Format Data from OleDbDataReader : OleDbDataReader « Database ADO.net « C# / C Sharp






Format Data from OleDbDataReader


using System;
using System.Data; 
using System.Data.OleDb; 

class OleDbProvider{
   static void Main(string[] args)  {
      String connString = "provider=sqloledb;server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI";
    
    String sql = "select firstname, lastname from Employee";

      OleDbConnection conn = null;
      OleDbDataReader reader = null;

      try {
         conn = new OleDbConnection(connString);
         conn.Open();

         OleDbCommand cmd = new OleDbCommand(sql, conn);
         reader = cmd.ExecuteReader();
         Console.WriteLine("Querying database {0} with query {1}\n"
                           , conn.Database
                           , cmd.CommandText);
         Console.WriteLine("First Name\tLast Name\n");

         while(reader.Read()){
            Console.WriteLine(
               "{0} | {1}" 
               , reader["FirstName"].ToString().PadLeft(10) 
               , reader[1].ToString().PadLeft(10)
               );
         }
      } catch (Exception e) {
         Console.WriteLine("Error: " + e);
      } finally {
         reader.Close();
         conn.Close();
      }
   }
}

           
       








Related examples in the same category

1.Query more columns and read result setQuery more columns and read result set
2.Read result set containing all columnsRead result set containing all columns
3.Execute complicated SQL query
4.Read value using OleDbDataReaderRead value using OleDbDataReader
5.Map database column name to OleDbDataReader
6.Reference data in OleDbDataReader by column name