本文整理匯總了C#中System.Data.SqlClient.SqlDataReader類的典型用法代碼示例。如果您正苦於以下問題:C# SqlDataReader類的具體用法?C# SqlDataReader怎麽用?C# SqlDataReader使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
SqlDataReader類屬於System.Data.SqlClient命名空間,在下文中一共展示了SqlDataReader類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Main
//引入命名空間
using System;
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main()
{
string str = "Data Source=(local);Initial Catalog=Northwind;"
+ "Integrated Security=SSPI";
ReadOrderData(str);
}
private static void ReadOrderData(string connectionString)
{
string queryString =
"SELECT OrderID, CustomerID FROM dbo.Orders;";
using (SqlConnection connection =
new SqlConnection(connectionString))
{
SqlCommand command =
new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
// Call Read before accessing data.
while (reader.Read())
{
ReadSingleRow((IDataRecord)reader);
}
// Call Close when done reading.
reader.Close();
}
}
private static void ReadSingleRow(IDataRecord record)
{
Console.WriteLine(String.Format("{0}, {1}", record[0], record[1]));
}
}