Csharp/C Sharp by API/System.Data.SqlClient/SqlDataReader

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

SqlDataReader.GetBoolean

  


using System;
using System.Data;
using System.Data.SqlClient;
class StronglyTypedColumnValues {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
        mySqlCommand.rumandText =
          "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");
        Console.WriteLine("ProductID .NET type = " + productsSqlDataReader.GetFieldType(productIDColPos));
        Console.WriteLine("ProductName .NET type = " + productsSqlDataReader.GetFieldType(productNameColPos));
        Console.WriteLine("UnitPrice .NET type = " + productsSqlDataReader.GetFieldType(unitPriceColPos));
        Console.WriteLine("UnitsInStock .NET type = " + productsSqlDataReader.GetFieldType(unitsInStockColPos));
        Console.WriteLine("Discontinued .NET type = " + productsSqlDataReader.GetFieldType(discontinuedColPos));
        Console.WriteLine("ProductID database type = " + productsSqlDataReader.GetDataTypeName(productIDColPos));
        Console.WriteLine("ProductName database type = " + productsSqlDataReader.GetDataTypeName(productNameColPos));
        Console.WriteLine("UnitPrice database type = " + productsSqlDataReader.GetDataTypeName(unitPriceColPos));
        Console.WriteLine("UnitsInStock database type = " + productsSqlDataReader.GetDataTypeName(unitsInStockColPos));
        Console.WriteLine("Discontinued database type = " + productsSqlDataReader.GetDataTypeName(discontinuedColPos));
        while (productsSqlDataReader.Read()) {
            int productID = productsSqlDataReader.GetInt32(productIDColPos);
            Console.WriteLine("productID = " + productID);
            string productName = productsSqlDataReader.GetString(productNameColPos);
            Console.WriteLine("productName = " + productName);
            decimal unitPrice = productsSqlDataReader.GetDecimal(unitPriceColPos);
            Console.WriteLine("unitPrice = " + unitPrice);
            short unitsInStock = productsSqlDataReader.GetInt16(unitsInStockColPos);
            Console.WriteLine("unitsInStock = " + unitsInStock);
            bool discontinued = productsSqlDataReader.GetBoolean(discontinuedColPos);
            Console.WriteLine("discontinued = " + discontinued);
        }
        productsSqlDataReader.Close();
        mySqlConnection.Close();
    }
}


SqlDataReader.GetDateTime

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

class Report {
    static void Main(string[] args) {
        SqlConnection dataConnection = new SqlConnection();
        try {
            dataConnection.ConnectionString = "Integrated Security=true;Initial Catalog=Northwind;Data Source=.\\SQLExpress";
            dataConnection.Open();
            SqlCommand dataCommand = new SqlCommand();
            dataCommand.Connection = dataConnection;
            dataCommand.rumandText =
                "SELECT OrderID, OrderDate, " +
                "ShippedDate, ShipName, ShipAddress, ShipCity, " +
                "ShipCountry FROM Orders WHERE CustomerID="0001"";
            Console.WriteLine("About to execute: {0}\n\n", dataCommand.rumandText);
            SqlDataReader dataReader = dataCommand.ExecuteReader();
            while (dataReader.Read()) {
                int orderId = dataReader.GetInt32(0);
                if (dataReader.IsDBNull(2)) {
                    Console.WriteLine("Order {0} not yet shipped\n\n", orderId);
                } else {
                    DateTime orderDate = dataReader.GetDateTime(1);
                    DateTime shipDate = dataReader.GetDateTime(2);
                    string shipName = dataReader.GetString(3);
                    string shipAddress = dataReader.GetString(4);
                    string shipCity = dataReader.GetString(5);
                    string shipCountry = dataReader.GetString(6);
                    Console.WriteLine(
                        "Order: {0}\nPlaced: {1}\nShipped: {2}\n" +
                        "To Address: {3}\n{4}\n{5}\n{6}\n\n", orderId, orderDate,
                        shipDate, shipName, shipAddress, shipCity, shipCountry);
                }
            }
            dataReader.Close();
        } catch (SqlException e) {
            Console.WriteLine("Error accessing the database: {0}", e.Message);
        } finally {
            dataConnection.Close();
        }
    }
}


SqlDataReader.GetFieldType

  

using System;
using System.Data;
using System.Data.SqlClient;
class StronglyTypedColumnValues {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
        mySqlCommand.rumandText =
          "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");
        Console.WriteLine("ProductID .NET type = " + productsSqlDataReader.GetFieldType(productIDColPos));
        Console.WriteLine("ProductName .NET type = " + productsSqlDataReader.GetFieldType(productNameColPos));
        Console.WriteLine("UnitPrice .NET type = " + productsSqlDataReader.GetFieldType(unitPriceColPos));
        Console.WriteLine("UnitsInStock .NET type = " + productsSqlDataReader.GetFieldType(unitsInStockColPos));
        Console.WriteLine("Discontinued .NET type = " + productsSqlDataReader.GetFieldType(discontinuedColPos));
        Console.WriteLine("ProductID database type = " + productsSqlDataReader.GetDataTypeName(productIDColPos));
        Console.WriteLine("ProductName database type = " + productsSqlDataReader.GetDataTypeName(productNameColPos));
        Console.WriteLine("UnitPrice database type = " + productsSqlDataReader.GetDataTypeName(unitPriceColPos));
        Console.WriteLine("UnitsInStock database type = " + productsSqlDataReader.GetDataTypeName(unitsInStockColPos));
        Console.WriteLine("Discontinued database type = " + productsSqlDataReader.GetDataTypeName(discontinuedColPos));
        while (productsSqlDataReader.Read()) {
            int productID = productsSqlDataReader.GetInt32(productIDColPos);
            Console.WriteLine("productID = " + productID);
            string productName = productsSqlDataReader.GetString(productNameColPos);
            Console.WriteLine("productName = " + productName);
            decimal unitPrice = productsSqlDataReader.GetDecimal(unitPriceColPos);
            Console.WriteLine("unitPrice = " + unitPrice);
            short unitsInStock = productsSqlDataReader.GetInt16(unitsInStockColPos);
            Console.WriteLine("unitsInStock = " + unitsInStock);
            bool discontinued = productsSqlDataReader.GetBoolean(discontinuedColPos);
            Console.WriteLine("discontinued = " + discontinued);
        }
        productsSqlDataReader.Close();
        mySqlConnection.Close();
    }
}


SqlDataReader.GetInt16

  


using System;
using System.Data;
using System.Data.SqlClient;
class StronglyTypedColumnValues {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
        mySqlCommand.rumandText =
          "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");
        Console.WriteLine("ProductID .NET type = " + productsSqlDataReader.GetFieldType(productIDColPos));
        Console.WriteLine("ProductName .NET type = " + productsSqlDataReader.GetFieldType(productNameColPos));
        Console.WriteLine("UnitPrice .NET type = " + productsSqlDataReader.GetFieldType(unitPriceColPos));
        Console.WriteLine("UnitsInStock .NET type = " + productsSqlDataReader.GetFieldType(unitsInStockColPos));
        Console.WriteLine("Discontinued .NET type = " + productsSqlDataReader.GetFieldType(discontinuedColPos));
        Console.WriteLine("ProductID database type = " + productsSqlDataReader.GetDataTypeName(productIDColPos));
        Console.WriteLine("ProductName database type = " + productsSqlDataReader.GetDataTypeName(productNameColPos));
        Console.WriteLine("UnitPrice database type = " + productsSqlDataReader.GetDataTypeName(unitPriceColPos));
        Console.WriteLine("UnitsInStock database type = " + productsSqlDataReader.GetDataTypeName(unitsInStockColPos));
        Console.WriteLine("Discontinued database type = " + productsSqlDataReader.GetDataTypeName(discontinuedColPos));
        while (productsSqlDataReader.Read()) {
            int productID = productsSqlDataReader.GetInt32(productIDColPos);
            Console.WriteLine("productID = " + productID);
            string productName = productsSqlDataReader.GetString(productNameColPos);
            Console.WriteLine("productName = " + productName);
            decimal unitPrice = productsSqlDataReader.GetDecimal(unitPriceColPos);
            Console.WriteLine("unitPrice = " + unitPrice);
            short unitsInStock = productsSqlDataReader.GetInt16(unitsInStockColPos);
            Console.WriteLine("unitsInStock = " + unitsInStock);
            bool discontinued = productsSqlDataReader.GetBoolean(discontinuedColPos);
            Console.WriteLine("discontinued = " + discontinued);
        }
        productsSqlDataReader.Close();
        mySqlConnection.Close();
    }
}


SqlDataReader.GetInt32

  


using System;
using System.Data;
using System.Data.SqlClient;
class StronglyTypedColumnValues {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
        mySqlCommand.rumandText =
          "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");
        Console.WriteLine("ProductID .NET type = " + productsSqlDataReader.GetFieldType(productIDColPos));
        Console.WriteLine("ProductName .NET type = " + productsSqlDataReader.GetFieldType(productNameColPos));
        Console.WriteLine("UnitPrice .NET type = " + productsSqlDataReader.GetFieldType(unitPriceColPos));
        Console.WriteLine("UnitsInStock .NET type = " + productsSqlDataReader.GetFieldType(unitsInStockColPos));
        Console.WriteLine("Discontinued .NET type = " + productsSqlDataReader.GetFieldType(discontinuedColPos));
        Console.WriteLine("ProductID database type = " + productsSqlDataReader.GetDataTypeName(productIDColPos));
        Console.WriteLine("ProductName database type = " + productsSqlDataReader.GetDataTypeName(productNameColPos));
        Console.WriteLine("UnitPrice database type = " + productsSqlDataReader.GetDataTypeName(unitPriceColPos));
        Console.WriteLine("UnitsInStock database type = " + productsSqlDataReader.GetDataTypeName(unitsInStockColPos));
        Console.WriteLine("Discontinued database type = " + productsSqlDataReader.GetDataTypeName(discontinuedColPos));
        while (productsSqlDataReader.Read()) {
            int productID = productsSqlDataReader.GetInt32(productIDColPos);
            Console.WriteLine("productID = " + productID);
            string productName = productsSqlDataReader.GetString(productNameColPos);
            Console.WriteLine("productName = " + productName);
            decimal unitPrice = productsSqlDataReader.GetDecimal(unitPriceColPos);
            Console.WriteLine("unitPrice = " + unitPrice);
            short unitsInStock = productsSqlDataReader.GetInt16(unitsInStockColPos);
            Console.WriteLine("unitsInStock = " + unitsInStock);
            bool discontinued = productsSqlDataReader.GetBoolean(discontinuedColPos);
            Console.WriteLine("discontinued = " + discontinued);
        }
        productsSqlDataReader.Close();
        mySqlConnection.Close();
    }
}


SqlDataReader.GetOrdinal

  

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.rumandText = "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();
  }
}


SqlDataReader.GetSchemaTable()

 

using System;
using System.Data;
using System.Data.SqlClient;
class SchemaOnlyCommandBehavior {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
        mySqlCommand.rumandText = "SELECT ProductID, ProductName, UnitPrice FROM Products WHERE ProductID = 1";
        mySqlConnection.Open();
        SqlDataReader productsSqlDataReader = mySqlCommand.ExecuteReader(CommandBehavior.SchemaOnly);
        DataTable myDataTable = productsSqlDataReader.GetSchemaTable();
        foreach (DataRow myDataRow in myDataTable.Rows) {
            foreach (DataColumn myDataColumn in myDataTable.Columns) {
                Console.WriteLine(myDataColumn + "= " + myDataRow[myDataColumn]);
                if (myDataColumn.ToString() == "ProviderType") {
                    Console.WriteLine(myDataColumn + "= " + ((System.Data.SqlDbType)myDataRow[myDataColumn]));
                }
            }
        }
        productsSqlDataReader.Close();
        mySqlConnection.Close();
    }
}


SqlDataReader.GetSqlBoolean

  

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
class StronglyTypedColumnValuesSql {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
        mySqlCommand.rumandText = "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()) {
            SqlInt32 productID = productsSqlDataReader.GetSqlInt32(productIDColPos);
            Console.WriteLine("productID = " + productID);
            SqlString productName = productsSqlDataReader.GetSqlString(productNameColPos);
            Console.WriteLine("productName = " + productName);
            SqlMoney unitPrice = productsSqlDataReader.GetSqlMoney(unitPriceColPos);
            Console.WriteLine("unitPrice = " + unitPrice);
            SqlInt16 unitsInStock = productsSqlDataReader.GetSqlInt16(unitsInStockColPos);
            Console.WriteLine("unitsInStock = " + unitsInStock);
            SqlBoolean discontinued = productsSqlDataReader.GetSqlBoolean(discontinuedColPos);
            Console.WriteLine("discontinued = " + discontinued);
        }
        productsSqlDataReader.Close();
        mySqlConnection.Close();
    }
}


SqlDataReader.GetSqlInt32

  

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
class StronglyTypedColumnValuesSql {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
        mySqlCommand.rumandText = "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()) {
            SqlInt32 productID = productsSqlDataReader.GetSqlInt32(productIDColPos);
            Console.WriteLine("productID = " + productID);
            SqlString productName = productsSqlDataReader.GetSqlString(productNameColPos);
            Console.WriteLine("productName = " + productName);
            SqlMoney unitPrice = productsSqlDataReader.GetSqlMoney(unitPriceColPos);
            Console.WriteLine("unitPrice = " + unitPrice);
            SqlInt16 unitsInStock = productsSqlDataReader.GetSqlInt16(unitsInStockColPos);
            Console.WriteLine("unitsInStock = " + unitsInStock);
            SqlBoolean discontinued = productsSqlDataReader.GetSqlBoolean(discontinuedColPos);
            Console.WriteLine("discontinued = " + discontinued);
        }
        productsSqlDataReader.Close();
        mySqlConnection.Close();
    }
}


SqlDataReader.GetSqlMoney

  

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
class StronglyTypedColumnValuesSql {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
        mySqlCommand.rumandText = "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()) {
            SqlInt32 productID = productsSqlDataReader.GetSqlInt32(productIDColPos);
            Console.WriteLine("productID = " + productID);
            SqlString productName = productsSqlDataReader.GetSqlString(productNameColPos);
            Console.WriteLine("productName = " + productName);
            SqlMoney unitPrice = productsSqlDataReader.GetSqlMoney(unitPriceColPos);
            Console.WriteLine("unitPrice = " + unitPrice);
            SqlInt16 unitsInStock = productsSqlDataReader.GetSqlInt16(unitsInStockColPos);
            Console.WriteLine("unitsInStock = " + unitsInStock);
            SqlBoolean discontinued = productsSqlDataReader.GetSqlBoolean(discontinuedColPos);
            Console.WriteLine("discontinued = " + discontinued);
        }
        productsSqlDataReader.Close();
        mySqlConnection.Close();
    }
}


SqlDataReader.GetSqlString

  

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
class StronglyTypedColumnValuesSql {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
        mySqlCommand.rumandText = "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()) {
            SqlInt32 productID = productsSqlDataReader.GetSqlInt32(productIDColPos);
            Console.WriteLine("productID = " + productID);
            SqlString productName = productsSqlDataReader.GetSqlString(productNameColPos);
            Console.WriteLine("productName = " + productName);
            SqlMoney unitPrice = productsSqlDataReader.GetSqlMoney(unitPriceColPos);
            Console.WriteLine("unitPrice = " + unitPrice);
            SqlInt16 unitsInStock = productsSqlDataReader.GetSqlInt16(unitsInStockColPos);
            Console.WriteLine("unitsInStock = " + unitsInStock);
            SqlBoolean discontinued = productsSqlDataReader.GetSqlBoolean(discontinuedColPos);
            Console.WriteLine("discontinued = " + discontinued);
        }
        productsSqlDataReader.Close();
        mySqlConnection.Close();
    }
}


SqlDataReader.GetString

  


using System;
using System.Data;
using System.Data.SqlClient;
class StronglyTypedColumnValues {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
        mySqlCommand.rumandText =
          "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");
        Console.WriteLine("ProductID .NET type = " + productsSqlDataReader.GetFieldType(productIDColPos));
        Console.WriteLine("ProductName .NET type = " + productsSqlDataReader.GetFieldType(productNameColPos));
        Console.WriteLine("UnitPrice .NET type = " + productsSqlDataReader.GetFieldType(unitPriceColPos));
        Console.WriteLine("UnitsInStock .NET type = " + productsSqlDataReader.GetFieldType(unitsInStockColPos));
        Console.WriteLine("Discontinued .NET type = " + productsSqlDataReader.GetFieldType(discontinuedColPos));
        Console.WriteLine("ProductID database type = " + productsSqlDataReader.GetDataTypeName(productIDColPos));
        Console.WriteLine("ProductName database type = " + productsSqlDataReader.GetDataTypeName(productNameColPos));
        Console.WriteLine("UnitPrice database type = " + productsSqlDataReader.GetDataTypeName(unitPriceColPos));
        Console.WriteLine("UnitsInStock database type = " + productsSqlDataReader.GetDataTypeName(unitsInStockColPos));
        Console.WriteLine("Discontinued database type = " + productsSqlDataReader.GetDataTypeName(discontinuedColPos));
        while (productsSqlDataReader.Read()) {
            int productID = productsSqlDataReader.GetInt32(productIDColPos);
            Console.WriteLine("productID = " + productID);
            string productName = productsSqlDataReader.GetString(productNameColPos);
            Console.WriteLine("productName = " + productName);
            decimal unitPrice = productsSqlDataReader.GetDecimal(unitPriceColPos);
            Console.WriteLine("unitPrice = " + unitPrice);
            short unitsInStock = productsSqlDataReader.GetInt16(unitsInStockColPos);
            Console.WriteLine("unitsInStock = " + unitsInStock);
            bool discontinued = productsSqlDataReader.GetBoolean(discontinuedColPos);
            Console.WriteLine("discontinued = " + discontinued);
        }
        productsSqlDataReader.Close();
        mySqlConnection.Close();
    }
}


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.rumandText = "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();
  }
}