查询没有给出正确的值

时间:2014-02-03 10:09:22

标签: c# mysql

SELECT 
    machine_id, operator_id, member_id, card_id, name, paid_amount, due_amount,
    paid_date, phone_number, @curRow := @curRow + 1 AS row_number 
FROM 
    transaction 
JOIN 
    (SELECT @curRow := 0) r where card_id='c1' order by Row_number desc limit 3 ;

当我在工作台中运行它时,它返回最后3条记录。但在我的代码中它只返回2条记录。有什么问题?

这是c#代码:

String query3 = "SELECT machine_id,operator_id,member_id,card_id,name,paid_amount,due_amount,paid_date,phone_number ,@curRow := @curRow + 1 AS row_number FROM transaction JOIN  (SELECT @curRow := 0) r where card_id=@card order by Row_number desc limit 4 ";
MySqlCommand command3 = new MySqlCommand(query3, con);
command3.Parameters.AddWithValue("@card", cardid);

using (MySqlDataReader rdr3 = command3.ExecuteReader())
{
    if (rdr3.Read())
    {
        while (rdr3.Read())
        {
            if (count == 1)
            {
                AMT1 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
                TOT1 = rdr3["paid_date"].ToString();
            }
            if (count == 2)
            {
                AMT2 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
                TOT2 = rdr3["paid_date"].ToString();
            }
            if (count == 3)
            {
                AMT3 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
                TOT3 = rdr3["paid_date"].ToString();
            }
            count++;
        }

        Response.Write("$AMT1=" + AMT1 + "|TOT1=" + TOT1 + "|AMT2=" + AMT2 + "|TOT2=" + TOT2 + "|AMT3=" + AMT3 + "|TOT3=" + TOT3 + "|TS=1#");
    }

2 个答案:

答案 0 :(得分:5)

您在开头使用Read()两次,然后以这种方式跳过第一条记录。从if

中删除一个
if (rdr3.Read())

你需要:

using (MySqlDataReader rdr3 = command3.ExecuteReader())
{
    while (rdr3.Read())
    {
        if (count == 1)
        {
            AMT1 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
            TOT1 = rdr3["paid_date"].ToString();
        }
        if (count == 2)
        {
            AMT2 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
            TOT2 = rdr3["paid_date"].ToString();
        }
        if (count == 3)
        {
            AMT3 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
            TOT3 = rdr3["paid_date"].ToString();
        }
        count++;
    }

    Response.Write("$AMT1=" + AMT1 + "|TOT1=" + TOT1 + "|AMT2=" + AMT2 + "|TOT2=" + TOT2 + "|AMT3=" + AMT3 + "|TOT3=" + TOT3 + "|TS=1#");
}

答案 1 :(得分:2)

使用if (rdr3.HasRows)代替if (rdr3.Read())

using (MySqlDataReader rdr3 = command3.ExecuteReader())
{
    if (rdr3.HasRows)
    {
        while (rdr3.Read())
        {
            if (count == 1)
            {
                AMT1 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
                TOT1 = rdr3["paid_date"].ToString();
            }
            if (count == 2)
            {
                AMT2 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
                TOT2 = rdr3["paid_date"].ToString();
            }
            if (count == 3)
            {
                AMT3 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
                TOT3 = rdr3["paid_date"].ToString();
            }
            count++;
        }
    }
       Else
        {
        Response.Write("No rows found.");
        }
        Response.Write("$AMT1=" + AMT1 + "|TOT1=" + TOT1 + "|AMT2=" + AMT2 + "|TOT2=" + TOT2 + "|AMT3=" + AMT3 + "|TOT3=" + TOT3 + "|TS=1#");
    }