下面的代码,为什么第1个不需要手动关闭数据库连接,第2个却需要?第2个不手动关闭连接行吗?
下面的代码,为什么第1个不需要手动关闭数据库连接,第2个却需要?第2个不手动关闭连接行吗?即把
1).
System.Data.IDataReader dataReader = dbCommand.ExecuteReader
(System.Data.CommandBehavior.CloseConnection);
改成
System.Data.IDataReader dataReader = dbCommand.ExecuteReader();
System.Data.DataSet GetProductsDataSet() {
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data
Source=C:\\BEGASPNET11\\ " +
"data\\Northwind.mdb ";
System.Data.IDbConnection dbConnection = new System.Data.OleDb.OleDbConnection
(connectionString);
string queryString = "SELECT [Products].[ProductName], [Categories].[CategoryName] FROM
[Products], [Ca " +
"tegories] WHERE ([Categories].[CategoryID] = [Products].[CategoryID]) ";
System.Data.IDbCommand dbCommand = new System.Data.OleDb.OleDbCommand();
dbCommand.CommandText = queryString;
dbCommand.Connection = dbConnection;
System.Data.IDbDataAdapter dataAdapter = new System.Data.OleDb.OleDbDataAdapter();
dataAdapter.SelectCommand = dbCommand;
System.Data.DataSet dataSet = new System.Data.DataSet();
dataAdapter.Fill(dataSet);
return dataSet;
}
2).
System.Data.IDataReader GetProductsReader() {
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data
Source=C:\\BEGASPNET11\\ " +
"data\\Northwind.mdb ";
System.Data.IDbConnection dbConnection = new System.Data.OleDb.OleDbConnection
(connectionString);
string queryString = "SELECT [Products].[ProductName], [Products].[QuantityPerUnit],