Reference result set by column index in SqlDataReader : SqlDataReader « ADO.Net « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / CSharp Tutorial » ADO.Net » SqlDataReader 




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

class MainClass
{
   static void Main(string[] args)
   {
      string connString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";

      string sql = @"select * from employee";

      SqlConnection conn = null;
      SqlDataReader reader = null;

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

         SqlCommand cmd = new SqlCommand(sql, conn);
         reader = cmd.ExecuteReader();

         Console.WriteLine("Querying database {0} with query {1}\n", conn.Database, cmd.CommandText);

         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();
      }
   }
}














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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.