jjzjj

c# - 错误 'there is already an open datareader associated with this command which must be closed first'

coder 2023-07-11 原文

运行时错误“已经有一个与此命令关联的打开的数据读取器必须先关闭”

objCommand = new SqlCommand("SELECT field1, field2 FROM sourcetable", objConn);

objDataReader = objCommand.ExecuteReader();

while (objDataReader.Read())
{
objInsertCommand = new SqlCommand("INSERT INTO tablename (field1, field2) VALUES (3, '" + objDataReader[0] + "')", objConn);
objInsertCommand.ExecuteNonQuery();//Here is the error
}
objDataReader.Close();

我不能在这里定义任何存储过程。 我们将不胜感激。

最佳答案

无需做所有这些,只需打开 MARS,您的问题就会得到解决。在您的连接字符串中只需添加 MultipleActiveResultSets=True;

关于c# - 错误 'there is already an open datareader associated with this command which must be closed first',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3839569/

有关c# - 错误 'there is already an open datareader associated with this command which must be closed first'的更多相关文章

随机推荐