本文整理汇总了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();
}
}
}