Important Issue About DataReader and DataGrid's Paging

使用DataReader的重要提示:
在Reader没有读完数据之前, Reader是不会自动关闭的。

Portal的DAL中大量使用了SqlDataReader用在DataBinding中以提高效率, 不知道有没有注意到在使用中都显式的关闭了DataReader. 若数据在DataBinding的过程中全部读出,可以不需要手动去关闭DataReader。但是如果数据在读出过程中中断(这种情况经常出现),就一定要手动关闭DataReader。

比如说,由于DataGrid的绑定DataTable进行默认分页太慢,数据量太大,消耗内存太多而使用DataReader进行CustomPaging的时候,绝对不能如同DataTable一般的直接取出全部数据绑定,也就是说,不能取出数量大于PageCount的数据。因为DataGrid只绑定PageCount个内容, 而你的DataReader要读的内容大于需要的内容,所以DataReader在绑定结束后,DataReader.Read()依然返回true, 这样DataReader是不会自动关闭自己及连接。这样会造成严重的资源泄露。

因此,如果想偷懒。就一定要算好数据量再读取,不要忽视Reader没有读完的情况。如果想要稳定,就一定要记得在任何时候, 用完了DataReader一定要关闭(如果没有CommandBehavior.CloseConnection, 还要关闭连接)。 使用DataReader进行数据绑定在不分页能完全显示的情况下是非常优越的,不需要处理细节问题。但是在数据需要分页的时候,不管怎么样,请你记得关闭DataReader, 或者是计算好你的数据。

再贴一个来自MS ADO.NET (Core Ref)中的QA

Q. I called a stored procedure that returns a set of rows. Everything seems to work except that the output and return parameters are empty. Why is that?

A. You can think of a stored procedure as a function in your code. The function doesn’t return a value until it has executed all of its code. If the stored procedure returns results and you haven’t finished processing these results, the stored procedure hasn’t really finished executing. Until you’ve closed the DataReader, the return and output parameters of your Command won’t contain the values returned by your stored procedure.

Let’s say we have the stored procedure

CREATE PROCEDURE RowsAndOutput (@OutputParam int OUTPUT) AS
    SELECT @OutputParam = COUNT(*) FROM Customers
    SELECT CustomerID, CompanyName, ContactName, Phone FROM Customers

and we call it with the following code:

 
 
None.gif string  strConn  =   " Provider=SQLOLEDB;Data Source=(local)\\NetSDK; "   +  
None.gif                 
" Initial Catalog=Northwind;Trusted_Connection=Yes; " ;
None.gifOleDbConnection cn 
=   new  OleDbConnection(strConn);
None.gifcn.Open();
None.gif
None.gif
string  strSQL  =   " {CALL RowsAndOutput(?)} " ;
None.gifOleDbCommand cmd 
=   new  OleDbCommand(strSQL, cn);
None.gifOleDbParameter param;
None.gifparam 
=  cmd.Parameters.Add( " @OutputParam " , OleDbType.Integer);
None.gifparam.Direction 
=  ParameterDirection.Output;
None.gifOleDbDataReader rdrCustomers 
=  cmd.ExecuteReader();
None.gifConsole.WriteLine(
" After execution -  "   +  ( string ) param.Value);
None.gif
while  (rdrCustomers.Read())
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {}
None.gifConsole.WriteLine(
" After reading rows -  "   +  ( string ) param.Value);
None.gif
while  (rdrCustomers.NextResult())
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {}
None.gifConsole.WriteLine(
" After reading all results -  "   +  
None.gif                  (
string ) param.Value);
None.gifrdrCustomers.Close();
None.gifConsole.WriteLine(
" After closing DataReader -  "   +  
None.gif                  (
string ) param.Value);

Even though the stored procedure sets the value of the output parameter before running the query that returns rows from the Customers table, the value of the output parameter is not available until after the DataReader is closed.

转载于:https://www.cnblogs.com/earthharp/archive/2004/10/16/52976.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值