c#MySQL第二个DataReader在DataReader while循环中
作者:互联网
正如你可能从标题中猜到的那样,我试图这样做:
#region check new nations
private void checknewnations()
{
addtolog("server","Checking for new nations");
string sql = "SELECT * FROM " + variables.tbl_nations + " WHERE nations_new=0";
MySqlCommand cmd = new MySqlCommand(sql, connection);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
addtolog("mysql",reader["nations_name"].ToString());
int nation_ID = int.Parse(reader["nations_ID"].ToString());
string nation_name = reader["nations_name"].ToString();
string user_ID = reader["nations_user"].ToString();
addnation(nation_ID, nation_name, user_ID);
}
addtolog("server","Finished checking for new nations.");
//connection.Close();
reader.Dispose();
}
#endregion
在while循环中调用它:
#region addnation
private void addnation(int nationIDnot, string nationName, string userID)
{
string nationID = makesixdigits(nationIDnot);
string userName = "";
string sql = "SELECT * FROM " + variables.tbl_users + " WHERE users_ID=" + userID;
MySqlCommand cmd = new MySqlCommand(sql, connection);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
userName = reader["users_name"].ToString();
}
reader.Dispose();
addtolog("add", "[" + nationID.ToString() + "] " + nationName + " [" + userID + "] " + userName);
}
#endregion
这给了我第二个代码块(在while循环中调用的代码块)中的错误,表示已经存在与该连接相关联的datareader.我怎么能让它工作,因为我相信有办法.
解决方法:
在2008连接字符串中,您可以通过MultipleActiveResultSets = true获得多个活动结果集;我没有使用mySQL所以我不确定,但也许你使用的是ADO提供商.
否则,只需建立另一个连接 – 不要重复使用同一个连接.
标签:datareader,mysql,connection,database,c-2 来源: https://codeday.me/bug/20190901/1786534.html