Use SqlDataReader to retrieve data in the resultset : SqlDataReader « ADO.Net « C# / CSharp Tutorial





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

class MainClass
{
  static void Main(string[] args)
  {
        string SQL = "SELECT * FROM Employee";

    string ConnectionString ="server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";
    SqlConnection conn = new SqlConnection(ConnectionString);

        SqlCommand cmd = new SqlCommand(SQL, conn);

        conn.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        try 
        {
            while (reader.Read()) 
            {
                Console.Write("ID:"+reader.GetInt32(0).ToString() );
                Console.Write(" ,");
                Console.WriteLine("Name:" + reader.GetString(1).ToString() );
            }
        }
        finally 
        {
            reader.Close();
            conn.Close();
        }   
  }
}
ID:2 ,Name:G










32.26.SqlDataReader
32.26.1.SqlDataReader
32.26.2.Use SQL Server Data Provider
32.26.3.Reference result set by column name in SqlDataReader
32.26.4.Reference result set by column index in SqlDataReader
32.26.5.Ordinal Indexer demo
32.26.6.Use loop to read all data in SqlDataReader
32.26.7.Use SqlDataReader to retrieve data in the resultset
32.26.8.Get data by data type using SqlDataReader
32.26.9.Using a DataReader to read row by row
32.26.10.Updating Data Using CommondBuilder
32.26.11.SqlDataReader: RecordsAffected
32.26.12.Typed Accessors: int, varchar and decimal
32.26.13.SqlDataReader.GetString, GetDateTime
32.26.14.how to read a table schema
32.26.15.Use indexer to read data
32.26.16.Mapping .NET Data Provider Data Types to .NET Framework Data Types by calling the GetXXX method
32.26.17.Mapping .NET Data Provider Data Types to .NET Framework Data Types by casting
32.26.18.Mapping .NET Data Provider Data Types to .NET Framework Data Types by converting
32.26.19.Retrieving a Result Set Stream Using a DataReader
32.26.20.Retrieves data as native SQL Server types.
32.26.21.Schema Table from SqlDataReader
32.26.22.ResultSet Info
32.26.23.Output fields from DataReader row
32.26.24.Ordinal Indexer
32.26.25.Get the column ordinal for the Phone attribute and use it to output the column
32.26.26.Data Reading with SqlDataReader
32.26.27.Accessing Data Values in a DataReader
32.26.28.DataReader has records using HasRows property
32.26.29.Handle Multiple Results
32.26.30.Determining the Number of Records Returned in a DataReader