Using the DataReader with OleDbConnection - CSharp Database

CSharp examples for Database:Connection

Description

Using the DataReader with OleDbConnection

Demo Code

using System;//from   w  w  w. ja  v  a 2 s .  c  om
using System.Data;
using System.Data.OleDb;
public class ReadVids
{
   public static void Main()
   {
      string  myConnectionString ="Provider=Microsoft.Jet.OLEDB.4.0;" +"User Id=;Password=;" +@"Data Source=C:\Videos.mdb";
      decimal total = 0;
      int     count = 0;
      string  mySelectQuery = "SELECT * FROM videos Order By Title";
      OleDbConnection myConnection =
      new OleDbConnection(myConnectionString);
      OleDbCommand myCommand =
      new OleDbCommand(mySelectQuery, myConnection);
      myConnection.Open();
      OleDbDataReader myDataReader = null;
      myDataReader = myCommand.ExecuteReader();
      while ( myDataReader.Read() )
      {
         Console.WriteLine(myDataReader.GetString(9).PadLeft(4,' ') +" - " +myDataReader.GetString(1) +" (" +myDataReader.GetString(2) +") - {0:C}", myDataReader.GetDecimal(3) );
         total += myDataReader.GetDecimal(3);
         count++;
      }

      myDataReader.Close();

      myConnection.Close();
      Console.WriteLine("\nTOTAL: {0:C}   AVG PRICE: {1:C}", total, total/count);
   }
}

Result


Related Tutorials