按货币排序后输出为Int行号

时间:2015-02-14 01:53:48

标签: c#

我是C#的新手。我想帮助完成代码以执行查询以返回订单后的行号(货币)。该表具有用户(人名)和货币(其点数)。查询将按货币对数据库进行排序,然后返回该用户的行号(字符串输入)。

  


  用户|货币
  人名| #

这是我到目前为止用谷歌搜索。

 public int overallrank(string user)
    {
        String sql = "SELECT (SELECT COUNT(*) FROM " + channel + " AS chan2 WHERE chan2.currency > chan1.currency ) as ChanRank FROM " + channel + " as chan1 WHERE chan1.user = ' " + user + "'";
        cmd = new SQLiteCommand(sql, myDB);
        var result = cmd.ExecuteScalar();
        return result == DBNull.Value ? 0 : (int)result;
    }

1 个答案:

答案 0 :(得分:0)

正如@juharr在评论中指出的那样,ExecuteScalar期待单行和单列。在该单元格中,您需要从数据库中获取排名。目前,您正在选择chan1中的所有列以及排名。

要获得排名,您可以像这样修改代码:

public long overallrank(string user)
{
    String sql = "SELECT (SELECT COUNT(*) FROM " + channel + " AS chan2 WHERE chan2.currency > chan1.currency ) as ChanRank FROM " + channel + " as chan1 WHERE chan1.user = '" + user +"'";
    using (cmd = new SQLiteCommand(sql, myDB))
    {
       var result = cmd.ExecuteScalar();
       return result == DBNull.Value ? 0L : (long)result;
    }
}