MySqlDataReader将总是返回假,即使数据没有在Asp.net [英] MySqlDataReader always Returning False even though data is there In Asp.net

查看:218
本文介绍了MySqlDataReader将总是返回假,即使数据没有在Asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

斐伊川家伙!
             我做了一个C#类,并在Asp.net..In我的C#文件一个处理我做了一个功能,该功能如下:

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

在我的处理程序文件我在这里调用这个函数是我的code ...

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

但DataReader的未读它总是返回false.Plz家伙帮我记录
排序的问题

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

推荐答案

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

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

其次你正在试图执行既有NonQuery(即不查询的数据的命令),并使用相同的阅读器的查询。这是否会导致例外,我不知道,但至少没有理由做两件事。你可以删除这两个问题

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

这篇关于MySqlDataReader将总是返回假,即使数据没有在Asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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