Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net dataReader是否会保持为空,从而导致;当读卡器关闭时调用元数据的尝试无效";?_Asp.net_Data Access Layer - Fatal编程技术网

Asp.net dataReader是否会保持为空,从而导致;当读卡器关闭时调用元数据的尝试无效";?

Asp.net dataReader是否会保持为空,从而导致;当读卡器关闭时调用元数据的尝试无效";?,asp.net,data-access-layer,Asp.net,Data Access Layer,我编写了这个方法,这样我就不必每次都编写开放连接等代码 public static bool TryExecuteReader(string commandText,string functionNameForLogging, string errorText, out SqlDataReader dataReader) { bool success = false; dataReader = null;

我编写了这个方法,这样我就不必每次都编写开放连接等代码

 public static bool TryExecuteReader(string commandText,string functionNameForLogging, string errorText, out SqlDataReader dataReader)
        {
            bool success = false;
            dataReader = null;
            try
            {
                using (SqlConnection sqlConnection = SqlUtilities.CreateSqlConnection())
                {
                    sqlConnection.Open();
                    SqlCommand sqlCommand = sqlConnection.CreateCommand();
                    sqlCommand.CommandText = commandText;
                    dataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);                    
                }
                success = true;
            }
            catch (Exception ex)
            {
                SqlUtilities.LogError(functionNameForLogging,ex.Message,-1);
            }

            return success;
        }

我认为这应该可以正常工作,但最近我遇到了错误“当阅读器关闭时调用元数据的尝试无效”。现在我不知道为什么会发生这种错误。但这似乎是最可能的原因。已经有一段时间了,所以我不能确定错误是否是由这种方式引起的。一旦using块结束,datareader是否保持为空?

一旦using块结束,datareader将关闭。 因此,任何从读取器读取数据的尝试都会引发异常。 请记住,datareader正在使用db连接。当连接关闭时(在using语句之后),将调用SQLConnection上的Dispose,从而使SqlDataReader nothing/null

将您的代码放在using语句之外进行连接,如下所示

SqlConnection sqlConnection = SqlUtilities.CreateSqlConnection()
using(SqlCommand sqlCommand = sqlConnection.CreateCommand())
 {
  sqlCommand.CommandText = commandText; 
  dataReader = sqlCommand.ExecuteReader   (CommandBehavior.CloseConnection);                     
 }
conn.Open();
success=true;
因为您已指定:

CommandBehavior.CloseConnection

当您在关闭数据读取器的那一刻执行命令对象时,与数据库的连接也将关闭

如果需要,可以让函数返回datareader而不是布尔值。这取决于你的喜好。但是,如果返回数据读取器,则可以使用
HasRows
Read
方法进行检查。然后,您可以将datareader包装在using语句中,例如:

using (SqlDataReader myDataReader = GetMyDataReader())
 {
   //do something with the reader
 }
myDataReader
的using语句末尾,将自动调用Dispose()
。由于您使用
命令行为调用了
.ExecuteReader()
内部的
GetMyDataReader()
。CloseConnection
,那么处理读取器也将关闭您的db连接。

阅读文章:

当使用
using
块时,您正在关闭连接,因此出现错误

而不是设置命令行为

  • CommandBehavior.CloseConnection

    当您将上述值作为参数传递给ExecuteReader时

  • 无需显式关闭连接。当您关闭读卡器时,请关闭连接

    //无需关闭连接,只需编写 reader.Close()

  • 当您将读取器传递给另一种处理数据的方法时,它将非常有用


但是dataReader是在using块之外声明的。而是在外部声明并传递给此方法。@sassyboy-这无关紧要,因为您正在命令对象中使用连接sqlConnection。使用sqlConnection对象的命令对象用于
.ExecuteReader
。这意味着datareader正在使用sqlConnection对象。因此,发生的事情是在sqlConnection对象的using语句被处理后立即发生的,这意味着命令对象被处理完毕,数据读取器被处理完毕。