use the GetOrdinal() from DataReader to get the numeric positions of a column : SqlDataReader « Database ADO.net « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Database ADO.net » SqlDataReaderScreenshots 
use the GetOrdinal() from DataReader to get the numeric positions of a column
 

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

class UsingColumnOrdinals {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");

        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();

        mySqlCommand.CommandText =
          "SELECT TOP 5 ProductID, ProductName, UnitPrice, " +
          "UnitsInStock, Discontinued " +
          "FROM Products " +
          "ORDER BY ProductID";

        mySqlConnection.Open();

        SqlDataReader productsSqlDataReader = mySqlCommand.ExecuteReader();

        int productIDColPos = productsSqlDataReader.GetOrdinal("ProductID");
        int productNameColPos = productsSqlDataReader.GetOrdinal("ProductName");
        int unitPriceColPos = productsSqlDataReader.GetOrdinal("UnitPrice");
        int unitsInStockColPos = productsSqlDataReader.GetOrdinal("UnitsInStock");
        int discontinuedColPos = productsSqlDataReader.GetOrdinal("Discontinued");

        while (productsSqlDataReader.Read()) {
            Console.WriteLine("ProductID = " + productsSqlDataReader[productIDColPos]);
            Console.WriteLine("ProductName = " + productsSqlDataReader[productNameColPos]);
            Console.WriteLine("UnitPrice = " + productsSqlDataReader[unitPriceColPos]);
            Console.WriteLine("UnitsInStock = " + productsSqlDataReader[unitsInStockColPos]);
            Console.WriteLine("Discontinued = " + productsSqlDataReader[discontinuedColPos]);
        }

        productsSqlDataReader.Close();
        mySqlConnection.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.how to read column values as C# types using the Get* methods
4.read column values as Sql* types using the GetSql* methods
5.Read data from SqlDataReader
6.Reference data in SqlDataReader by column name
7.Use A Data Reader
8.Use While loop to read query result data from SqlDataReader
9.Deal with Multiple Results
10.SqlDataReader Ordinal Indexer
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.