我收到以下错误消息:
System.InvalidOperationException: ExecuteReader需要一个开放且可用的连接。连接的当前状态为已关闭。
下面是我的代码:
public IDataReader ExecuteReader()
{
IDataReader reader = null;
try
{
this.Open();
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return reader;
}有人知道我该怎么解决这个问题吗?
发布于 2009-07-21 11:51:28
尚未打开SQLCommand附加到的连接对象。必须先打开连接,然后才能查询。
如下所示:
private static void OpenSqlConnection(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
var cmd = connection.CreateCommand();
// Do your command access here.
}
}https://stackoverflow.com/questions/1158655
复制相似问题