尝试无效时,读者是封闭阅读 [英] Invalid attempt to Read when reader is closed

查看:195
本文介绍了尝试无效时,读者是封闭阅读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的C#和MySQL的要求。我想找回我的DATAS在我的分贝,但我有此错误消息:无效的尝试读取时读取器已关闭



感谢您的帮助家伙:)



我有这个功能:

 公开了MySqlDataReader GetValueFromTable(字符串表, ArrayList的attribut,ArrayList的参数)
{
查询字符串=的String.Empty;
MySqlDataReader将RDR = NULL;

{
=查询SELECT * FROM`+表+'WHERE;
的for(int i = 0; I< attribut.Count;我++)
{
查询+ = attribut [I]作为字符串;
查询+ ==;
查询+ =参数[I]作为字符串;

如果(!I = attribut.Count - 1)
查询+ =和;
}

查询+ =;

使用(的MySqlConnection)
{
使用(的MySqlCommand =新的MySqlCommand(查询的MySqlConnection))
{
RDR = mysqlCommand.ExecuteReader();
}
}
}
赶上(异常前)
{
的debug.log(ex.ToString());
}
终于{}

返回RDR;
}

和在我的代码下一个地方我这样做:

  ArrayList的attribut =新的ArrayList(); 
ArrayList的参数=新的ArrayList();

attribut.Add(usern_id);
parameter.Add(1);
MySqlDataReader将读卡器= dataBase.GetValueFromTable(巴比,attribut,参数);
reader.Read();
的debug.log(读者[0]);
reader.Close();


解决方案

using块这里关闭连接(退出)

 使用(的MySqlCommand =新的MySqlCommand(查询的MySqlConnection))


I'm working on C# and MySql request. I'm trying to retrieve my datas in my db but I have this error message : Invalid attempt to Read when reader is closed.

Thanks for your help guys :)

I have this function :

public MySqlDataReader GetValueFromTable(string table, ArrayList attribut, ArrayList parameter)
{
   string query = string.Empty;
   MySqlDataReader rdr = null;      
   try
   {
      query = "SELECT * FROM `" + table + "` WHERE ";
      for (int i = 0; i < attribut.Count; i++)
      {
         query += attribut[i] as string;
         query += " = ";
         query += parameter[i] as string;

         if(i != attribut.Count - 1) 
            query += " AND ";
      }

      query += ";";

      using (mysqlConnection)
      {
          using (mysqlCommand = new MySqlCommand(query, mysqlConnection))
          {                  
             rdr = mysqlCommand.ExecuteReader();
          }   
      }
   }
   catch (Exception ex)
   {
      Debug.Log(ex.ToString());
   }
   finally {}

   return rdr;
}

And next somewhere in my code I doing this:

ArrayList attribut = new ArrayList();       
ArrayList parameter = new ArrayList();

attribut.Add("usern_id"); 
parameter.Add("1");     
MySqlDataReader reader = dataBase.GetValueFromTable("papillon", attribut, parameter);
reader.Read();
Debug.Log(reader[0]);
reader.Close();

解决方案

The using block closes the connection here (on exit)

using (mysqlCommand = new MySqlCommand(query, mysqlConnection))

这篇关于尝试无效时,读者是封闭阅读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆