即使数据在Asp.net中,MySqlDataReader总是返回False

时间:2013-01-30 13:49:13

标签: c# asp.net datareader

Hii伙计!!!              我在Asp.net中创建了一个C#类和一个处理程序。在我的C#文件中,我创建了一个函数,如下所示:

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);
}

在我的处理程序文件中,我在这里调用此函数是我的代码...

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);

但是datareader没有读取它总是返回false的记录.Plz伙伴帮助我 解决问题。

3 个答案:

答案 0 :(得分:0)

删除此行代码 cmd.ExecuteNonQuery();

编辑:ExecuteNonQuery用于UPDATEINSERT等DML语句。

阅读@Jon关于异常处理的评论。

答案 1 :(得分:0)

可能你的问题源于这个局部变量

MySqlConnection conn;

当您退出方法Consulta时,此变量将被销毁并且底层连接将被关闭。这将使您的MySqlDataReader无法使用,因为它需要打开连接

我将重构您的代码,以这种方式删除对Consulta的调用:

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())
               {
                  .....
               }
           }
       }
   }

答案 2 :(得分:0)

你至少有两个问题  第一个是你吞下任何例外。这可能会导致您的问题。如果有异常,很可能您无法从阅读器中读取会导致您的while循环从不执行。有一个原因是为什么空捕获是一个警告

其次,您正在尝试执行NonQuery(不查询数据的命令)和使用相同阅读器的查询。这是否会引起异常我不知道,但至少没有理由同时做到这两点。您可以删除这两个问题

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);
}