想不通异常消息想不通、异常、消息

由网友(孤独像条狗i)分享简介:我有以下错误消息,我使用petaPOCO。为什么我有这个错误消息,并且什么我做错了有这个消息:{已经有一个用此命令,必须先关闭相关联的打开的DataReader。}这是我已经能够复制的异常信息。System.InvalidOperationException了抓消息=已经有一个用此命令,必须先关闭相关联的打开的Data...

我有以下错误消息,我使用petaPOCO。为什么我有这个错误消息,并且什么我做错了有这个消息:

  {已经有一个用此命令,必须先关闭相关联的打开的DataReader。}
 

 这是我已经能够复制的异常信息。
 

  

System.InvalidOperationException了抓     消息=已经有一个用此命令,必须先关闭相关联的打开的DataReader。     来源= System.Data这     堆栈跟踪:          在System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand的命令)          在System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(字符串方法,SqlCommand的命令)          在System.Data.SqlClient.SqlCommand.ValidateCommand(字符串方法,布尔异步)          在System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,字符串方法,DbAsyncResult结果)          在System.Data.SqlClient.SqlCommand.RunExecuteReader(的CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔returnStream,字符串方法)          在System.Data.SqlClient.SqlCommand.ExecuteScalar()          在PetaPoco.Database.Insert(字符串tableName值,字符串primaryKeyName,布尔自动增量,对象POCO)在C:开发 code API 型号 PetaPoco.cs:行1243     的InnerException:

解决方案 烧脑情报串 00后穿鞋叫时尚 错 现在的时尚是穿塑料

下面是一个很好的解释为什么这引发异常:

http://blogs.msdn.com/b/spike/archive/2009/08/20/there-is-already-an-open-datareader-associated-with-this-command-which-must-be-closed-first-explained.aspx

结论是如下:

由于SqlDataReader对象保持内存流(结果集)可用,直到明确关闭SqlDataReader的,如果你尝试创建一个新的阅读器而不关闭previous一个你能得到这个例外。

改变你的code有一个using语句,只要你创建一个SqlDataReader:

 的SqlCommand CMD =新的SqlCommand(SQL,CON);
使用(SqlDataReader的RDR = cmd.ExecuteReader())
{
  而(rdr.Read())
  {
    Console.WriteLine(CID:{0},CTEXT:{1},RDR [0]的ToString(),RDR [1]的ToString());
  }
}
 

使用会自动调用Dispose()(其中关闭阅读器)时关闭(终止})就达到了。

如果这种异常在petaPOCO再有就是在他们的code错误或在未指定的方式,您使用的是code。

I am having the following error message and I am using petaPOCO. Why am I having this error message and What am I doing wrong to have this message:

{"There is already an open DataReader associated with this Command which must be closed first."}

This is what I have been able to copy for the exception message.

System.InvalidOperationException was caught Message=There is already an open DataReader associated with this Command which must be closed first. Source=System.Data StackTrace: at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command) at System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command) at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) at System.Data.SqlClient.SqlCommand.ExecuteScalar() at PetaPoco.Database.Insert(String tableName, String primaryKeyName, Boolean autoIncrement, Object poco) in C:DevCodeAPIModelsPetaPoco.cs:line 1243 InnerException:

解决方案

Here is an excellent explanation why this exception is raised:

http://blogs.msdn.com/b/spike/archive/2009/08/20/there-is-already-an-open-datareader-associated-with-this-command-which-must-be-closed-first-explained.aspx

The conclusion is as following:

Because the SqlDataReader keeps the memory stream (resultset) available until you explicitly close the SqlDataReader you can get this exception if you try to create a new reader without closing the previous one.

Alter your code to have a using statement whenever you create a SqlDataReader:

SqlCommand cmd = new SqlCommand(sql, con);
using (SqlDataReader rdr = cmd.ExecuteReader())
{
  while (rdr.Read())
  {
    Console.WriteLine("cid: {0}, ctext: {1}", rdr[0].ToString(), rdr[1].ToString());
  }
}

Using will automatically call dispose() (which closes the reader) when the closure (ending } ) is reached.

If this exception is raised in petaPOCO then there is a bug in their code or you are using the code in an unspecified way.

阅读全文

相关推荐

最新文章