阅读器关闭时 Read 的尝试无效
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data.SqlClient;
namespace DataExample
{
class Program
{
static void Main(string[] args)
{
string source ="Provider=SQLOLEDB;"+
"server=(local);" +
"integrated security=SSPI;" +
"database=Northwind";
string select = "SELECT ContactName,CompanyName from Customers";
OleDbConnection conn = new OleDbConnection(source);
conn.Open();
OleDbCommand cmd = new OleDbCommand(select, conn);
OleDbDataReader aReader = cmd.ExecuteReader();
while (aReader.Read()) //在此处提示阅读器关闭时 Read 的尝试无效
{
Console.WriteLine("'{0}'from{1}", aReader.GetString(0), aReader.GetString(1));
aReader.Close();
conn.Close();
}
Console.ReadLine();
}
}
}
在while(aReader.Read())处提示阅读器关闭时 Read 的尝试无效,我照着《C#高级编程》(第四版)第532页的代码调试的,怎么会出错?请赐教!