MySqlDataReader always Returning False even though data is there In Asp.net

*爱你&永不变心* 提交于 2020-01-06 07:56:29

问题


Hii Guys !!! I made one C# class and one handler in Asp.net..In My C# file I made a function which is as Below:

public MySqlDataReader Consulta(String sql){

    String error="";

    try 
    {

        string connectionString = "Server=*****;Port=3306;Database=db;UID=*****;Pwd=****;pooling=false";
        MySqlConnection conn;
        conn = new MySqlConnection(connectionString);
        conn.Open();

        //string s = "select empid,empname,authcode from authcode";
        MySqlCommand cmd = new MySqlCommand(sql,conn);

        //cmd.CommandText = s;
        cmd.ExecuteNonQuery();

        rs =cmd.ExecuteReader();

        } 
        catch (Exception e) 
        {
            //error = e.getMessage();
        }

        return (rs);
}

In My handler File i am calling this function here is my code...

MySqldataReader rs = conexiondb.Consulta(strQuery);

total = conexiondb.countRec("price", "processeddata_table");

string json;
json = json + "{\n";
json = json + " \"page\":\""+intpage+"\",\n";
json = json + "\"total\":"+total_pages+",\n";
json = json + "\"records\":"+total+",\n";
json = json + "\"rows\": [";
rc =false;

while(rs.Read()){

    if(rc){
        json = json + ",";
    }
    json = json + "\n{";
    json = json + "\"price\":\"" + Convert.ToInt32(rs["price"]) + "\",";
    json = json + "\"cell\":[" + Convert.ToInt32(rs["price"]) + "";
    json = json + ",\"" + Convert.ToString(rs["username"]) + "\"";
    json = json + ",\"" + Convert.ToString(rs["ordinal"]) + "\"";
    json = json + ",\"" + Convert.ToString(rs["authcode"]) + "\"";
    json = json + ",\"" + Convert.ToString(rs["extension"]) + "\"";
    json = json + ",\"" + Convert.ToString(rs["trunk"]) + "\"";
    json = json + ",\"" + Convert.ToString(rs["dialnumber"]) + "\"";
    json = json + ",\"" + Convert.ToString(rs["dialdate"]) + "\"";
    json = json + ",\"" + Convert.ToString(rs["dialtime"]) + "\"";
    json = json + ",\"" + Convert.ToString(rs["duration"]) + "\"";
    json = json + ",\"" + Convert.ToString(rs["destination"]) + "\"";
    json = json + ",\"" + Convert.ToString(rs["price"]) + "\"";
    json = json + ",\"" + Convert.ToString(rs["toc"]) + "\"]";
    json = json + "}";

    rc=true;
}
json = json +"]\n";

json = json +"}";

HttpContext.Current.Response.Write(json);

But datareader is not reading the records it is always Returning false.Plz guys Help me to sort the problem .


回答1:


You have at least two issues first one is that you are swallowing any exceptions. This probably causes your problem. If there was an exception it's very likely that you can't read from the reader which would result in your while loop never executing. There's a reason why having an empty catch is a warning

Secondly your are trying to execute both an NonQuery (an command that does not query data) and a query with the same reader. Whether this would cause an exception I don't know but at least there's no reason to do both. You could remove both issues

public MySqlDataReader Consulta(String sql){
            string connectionString = "Server=***;Port=3306;Database=****;UID=***;Pwd=****;pooling=false";
   var conn = new MySqlConnection(connectionString);
   conn.Open();

   var cmd = new MySqlCommand(sql,conn);
   var rs =cmd.ExecuteReader();
   return (rs);
}



回答2:


Remove this line of code cmd.ExecuteNonQuery();

EDIT: ExecuteNonQuery is used for DML statements such as UPDATE, INSERT.

Read comments by @Jon on exception handling.




回答3:


Probably your problem stems from this local variable

MySqlConnection conn;

when you exit from the method Consulta this variable get destroyed and the underlying connection is closed. This will render your MySqlDataReader unusable because it requires an open connection

I will refactor your code removing the call to Consulta in this way:

string sql = "your SELECT command here";
string connectionString = "Server=localhost;Port=3306;Database=projecttt;" +
                         "UID=root;Pwd=techsoft;pooling=false";

using(MySqlConnection conn = new MySqlConnection(connectionString))
{
     conn.Open();
     using(MySqlCommand cmd = new MySqlCommand(sql,conn))
     {
          using(MySqlDataReader rs =cmd.ExecuteReader())
          {
               total = conexiondb.countRec("price", "processeddata_table");

               string json;
               json = json + "{\n";
               json = json + " \"page\":\""+intpage+"\",\n";
               json = json + "\"total\":"+total_pages+",\n";
               json = json + "\"records\":"+total+",\n";
               json = json + "\"rows\": [";
               rc =false;
               while(rs.Read())
               {
                  .....
               }
           }
       }
   }


来源:https://stackoverflow.com/questions/14605621/mysqldatareader-always-returning-false-even-though-data-is-there-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!