在datareader和executenonquery中使用相同的连接 [英] use same connection in datareader and executenonquery
本文介绍了在datareader和executenonquery中使用相同的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用
I use
MySqlConnection addh = new MySqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);
addh.Open();
MySqlCommand myCommand = new MySqlCommand(myqry, addh);
然后
and then
MySqlDataReader myDataReader;
myDataReader = myCommand.ExecuteReader();
if (myDataReader.HasRows)
{
while (myDataReader.Read())
{
foreach(string ede in array)
{
MySqlCommand cmd = new MySqlCommand(amenity, addh);
int insert_result = cmd.ExecuteNonQuery();
}
}
myDataReader.Close();
addh.Close();
但我收到错误
But i get error as
C# mySQL There is already an open DataReader associated with this Connection which must be closed first
什么是问题
what is issue
推荐答案
当存在与之关联的打开的DataReader时,您不能使用相同的连接来执行任何其他数据库操作:它会弄乱从数据库返回的数据。这就像尝试更改List< string>从列表中正在处理的foreach
循环内部:它会导致问题,因此不允许这样做。
要么创建新连接,要么使用它,或保存数据以在阅读器关闭后进行操作。
You cannot use the same connection to perform any other database operation while there is an open DataReader associated with it: it would mess up the data returned from the database. It's just like trying to change a List<string> from inside aforeach
loop which is working on the list: it would cause problems so it isn't allowed.
Either create a new connection and use that, or save the data to do the operations after the reader has been closed.
这篇关于在datareader和executenonquery中使用相同的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文