SqlDataReader.GetDateTime : SqlDataReader « System.Data.SqlClient « C# / C Sharp by API






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.CommandText =
                "SELECT OrderID, OrderDate, " +
                "ShippedDate, ShipName, ShipAddress, ShipCity, " +
                "ShipCountry FROM Orders WHERE CustomerID='0001'";
            Console.WriteLine("About to execute: {0}\n\n", dataCommand.CommandText);

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

   
  








Related examples in the same category

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