SqlDataReader.Read() : SqlDataReader « System.Data.SqlClient « C# / C Sharp by API






SqlDataReader.Read()

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

class UsingColumnOrdinals
{
  public static void Main()
  {
    SqlConnection mySqlConnection =new SqlConnection("server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;");

    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();

    mySqlCommand.CommandText = "SELECT TOP 5 ID, FirstName, LastName FROM employee " +
      "ORDER BY ID";

    mySqlConnection.Open();

    SqlDataReader productsSqlDataReader = mySqlCommand.ExecuteReader();

    int idPos = productsSqlDataReader.GetOrdinal("ID");
    int firstNamePos = productsSqlDataReader.GetOrdinal("LastName");
    int lastNamePos = productsSqlDataReader.GetOrdinal("FirstName");

    while (productsSqlDataReader.Read())
    {
      Console.WriteLine("ID = " + productsSqlDataReader[idPos]);
      Console.WriteLine("FirstName = " + productsSqlDataReader[firstNamePos]);
      Console.WriteLine("LastName = " + productsSqlDataReader[lastNamePos]);
    }

    productsSqlDataReader.Close();
    mySqlConnection.Close();
  }
}

   
    
  








Related examples in the same category

1.SqlDataReader.GetBoolean
2.SqlDataReader.GetDateTime
3.SqlDataReader.GetFieldType
4.SqlDataReader.GetInt16
5.SqlDataReader.GetInt32
6.SqlDataReader.GetOrdinal
7.SqlDataReader.GetSchemaTable()
8.SqlDataReader.GetSqlBoolean
9.SqlDataReader.GetSqlInt32
10.SqlDataReader.GetSqlMoney
11.SqlDataReader.GetSqlString
12.SqlDataReader.GetString