Welcome to My blog

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

使用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:

  • string strConn = "Provider=SQLOLEDB;Data Source=(local)\\NetSDK;" + 
                     
    "Initial Catalog=Northwind;Trusted_Connection=Yes;";
    OleDbConnection cn 
    = new OleDbConnection(strConn);
    cn.Open();

    string strSQL = "{CALL RowsAndOutput(?)}";
    OleDbCommand cmd 
    = new OleDbCommand(strSQL, cn);
    OleDbParameter param;
    param 
    = cmd.Parameters.Add("@OutputParam", OleDbType.Integer);
    param.Direction 
    = ParameterDirection.Output;
    OleDbDataReader rdrCustomers 
    = cmd.ExecuteReader();
    Console.WriteLine(
    "After execution - " + (string) param.Value);
    while (rdrCustomers.Read())
    {}
    Console.WriteLine(
    "After reading rows - " + (string) param.Value);
    while (rdrCustomers.NextResult())
    {}
    Console.WriteLine(
    "After reading all results - " + 
                      (
    string) param.Value);
    rdrCustomers.Close();
    Console.WriteLine(
    "After closing DataReader - " + 
                      (
    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.

  • posted on 2004-10-16 01:35  Agent  阅读(2531)  评论(0编辑  收藏  举报