在SqlDataReader中使用类型访问器方法
// 在SqlDataReader中使用类型访问器方法
// TypedAccessors.cs
using System;
using System.Data;
using System.Data.SqlClient;
namespace Ch12
{
class TypedAccessors
{
static void Main( string[] args)
{
string strConn = "server=.\\MSSQL2012;integrated security=true;database=Northwind;";
string sql = "select top 5 productname, unitprice, unitsinstock, discontinued from products";
SqlConnection conn = new SqlConnection(strConn);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Console.WriteLine( "{0}\t {1}\t {2}\t {3}" ,
rdr.GetString(0).PadRight(30),
rdr.GetDecimal(1),
rdr.GetInt16(2),
rdr.GetBoolean(3));
}
rdr.Close();
}
catch( Exception ex)
{
Console.WriteLine( "发生错误:" + ex);
}
finally
{
conn.Close();
Console.ReadLine();
}
}
}
}
--------------------------------------------------------------
Chai 18.0000 39 False
Chang 19.0000 17 False
Aniseed Syrup 10.0000 13 False
Chef Anton's Cajun Seasoning 22.0000 53 False
Chef Anton's Gumbo Mix 21.3500 0 True
浙公网安备 33010602011771号