Typed Accessors : OleDbDataReader « ADO.Net « C# / CSharp Tutorial






using System;
using System.Data;
using System.Data.SqlClient;

    class TypedAccessors
    {
        static void Main(string[] args)
        {
            string connString = @"server = .\sqlexpress;integrated security = true;database = northwind";
            string sql = @"select productname, unitprice, unitsinstock,discontinued from products";
            SqlConnection conn = new SqlConnection(connString);
            try{
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    Console.WriteLine(rdr.GetString(0));
                    Console.WriteLine(rdr.GetDecimal(1));
                    Console.WriteLine(rdr.GetInt16(2));
                    Console.WriteLine(rdr.GetBoolean(3));
                }
                rdr.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error Occurred: " + e);
            }
            finally
            {
                conn.Close();
            } 
        }
    }








32.11.OleDbDataReader
32.11.1.Read result set from OleDbDataReader
32.11.2.Use to OleDbDataReader retrieve data
32.11.3.OleDbDataAdapter and SqlDataAdapter: UpdateOleDbDataAdapter and SqlDataAdapter: Update
32.11.4.Typed Accessors
32.11.5.Get SchemaTable from OleDbDataReader