Use A Data Reader : SqlDataReader « Database ADO.net « C# / C Sharp






Use A Data Reader


/*
 * C# Programmers Pocket Consultant
 * Author: Gregory S. MacBeth
 * Email: gmacbeth@comporium.net
 * Create Date: June 27, 2003
 * Last Modified Date:
 * Version: 1
 */
using System;
using System.Data;
using System.Data.SqlClient;

namespace Client.Chapter_13___ADO.NET
{
    public class UsingADataReader
    {
        static void Main(string[] args)
        {
            SqlConnection MyConnection = new SqlConnection(@"Data Source=(local); Initial Catalog = CaseManager; Integrated Security=true");

            MyConnection.Open();

            SqlCommand MyCommand = new SqlCommand("SELECT * FROM CaseInfo", MyConnection);
            SqlDataReader MyDataReader = MyCommand.ExecuteReader(CommandBehavior.CloseConnection);

            while (MyDataReader.Read())
            {
                Console.WriteLine(MyDataReader[0] + " " + MyDataReader[1]);
            }

            MyConnection.Close();
        }
    }
}


           
       








Related examples in the same category

1.Execute multiple SELECT statements using a SqlCommand object and read the results using a SqlDataReader object
2.Get column index from SqlDataReader
3.use the GetOrdinal() from DataReader to get the numeric positions of a column
4.how to read column values as C# types using the Get* methods
5.read column values as Sql* types using the GetSql* methods
6.Read data from SqlDataReader
7.Reference data in SqlDataReader by column name
8.Use While loop to read query result data from SqlDataReader
9.Deal with Multiple Results
10.SqlDataReader Ordinal Indexer