當前位置: 首頁>>代碼示例>>C#>>正文


C# SqlDataReader.GetDateTime方法代碼示例

本文整理匯總了C#中System.Data.SqlClient.SqlDataReader.GetDateTime方法的典型用法代碼示例。如果您正苦於以下問題:C# SqlDataReader.GetDateTime方法的具體用法?C# SqlDataReader.GetDateTime怎麽用?C# SqlDataReader.GetDateTime使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Data.SqlClient.SqlDataReader的用法示例。


在下文中一共展示了SqlDataReader.GetDateTime方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
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();
        }
    }
}
開發者ID:C#程序員,項目名稱:System.Data.SqlClient,代碼行數:51,代碼來源:SqlDataReader.GetDateTime


注:本文中的System.Data.SqlClient.SqlDataReader.GetDateTime方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。