如何使用t-sql中的where子句计算已过滤的行?

时间:2015-08-21 05:14:15

标签: c# jquery mysql sql tsql

鉴于下表。我需要计算 SubCategory 列中包含值的所有记录的行数" 儿童图书"使用transact SQL。在这种情况下,我期望得到2的结果。我试过这样的事情

SqlCommand cmd = new SqlCommand("Select Count(*) From [DisplayCenterTab] Where [SubCategory ]= '" + subcateg + "' ", con);

但它返回零(0)结果而不是2.我在这里缺少什么?感谢

我的表:

Id        ProductId       ProductName      Category            SubCategory
34534     34643645        dfhfsjfdjgh      sdfagdsfhfhgfhj     dfgsdhhgfh
45234     456436          fghdfjfgj        dfgsdhfhfgfgh       Children's Books
46536     45646           fgjdgjfgh        dfgshfgfdghj        Children's Books
43645     466456          systyerttry      sdhdfhfggjh         dfhshfdjgfgh
34526     456345          areyruuty        dfshfdfgjghj        dafgshfghgfh

更新

JQuery代码

$("#sellstuff").click(function () {
    var dataObject = $("#scrolldummy").text();
    //var dataObject = "Children's Books";
    $.getJSON("/OnlineStore/TotalNumberofSubCateg", dataObject, function (data) {
        $("#search").val(data + " Assorted Items for Sale.");
    });
})

C#代码

[HttpGet]
        public JsonResult TotalNumberofSubCateg(string subcateg)
        {
            int rowcount;
            string constr = ConfigurationManager.ConnectionStrings["StockConnString"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            con.Open();

            SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM DisplayCenterTab Where SubCategory = '"+ subcateg +"' ", con);

            cmd.CommandType = CommandType.Text;
            rowcount = Convert.ToInt32(cmd.ExecuteScalar());
            con.Close();
            return Json(rowcount, JsonRequestBehavior.AllowGet);
        }

4 个答案:

答案 0 :(得分:2)

您需要从subcateg变量中转义单引号字符。 试试这个

    SqlCommand cmd = 
new SqlCommand("Select Count(*) From [DisplayCenterTab] Where [SubCategory ]= '" + subcateg.Replace("'","''") + "' ", con);

答案 1 :(得分:1)

在查询中使用命令参数总是更好,它会处理所有内容。这是您重写的代码:

    public JsonResult TotalNumberofSubCateg(string subcateg)
    {
        int rowcount;
        string constr = ConfigurationManager.ConnectionStrings["StockConnString"].ConnectionString;
        SqlConnection con = new SqlConnection(constr);
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM DisplayCenterTab Where SubCategory = @S0 ", con);

        cmd.Parameters.AddWithValue("@S0", subcateg ); // it will handle the string format
        cmd.CommandType = CommandType.Text;
        rowcount = Convert.ToInt32(cmd.ExecuteScalar());
        con.Close();
        return Json(rowcount, JsonRequestBehavior.AllowGet);
    }

通常,这应该可行,但我还没有测试过。

答案 2 :(得分:0)

您需要在''中使用双引号 SQL替换单引号 SqlCommand cmd = new SqlCommand("Select Count(*) From [DisplayCenterTab] Where [SubCategory ]= 'Children''s Books' ", con) ,例如:

setTimeout(function(){
    window.location.href = "pagez.php";
}, 8000);

答案 3 :(得分:0)

伙计......实际上是我的烂摊子。这是我的问题的更新。我的sql语句实际上没有问题,但它实际上是我的客户端脚本变量声明。我忘了记住在你的客户端脚本中你的dataObject变量应该有你的类模型变量。我发布这个更新,所以任何人都不知道或者还没有能够遇到这类问题就会知道它们。

 $("#sellstuff").click(function () {

    var dataObject =
        { SubCategory: $("#scrolldummy").text() };

    $.getJSON("/OnlineStore/TotalNumberofSubCateg", dataObject, function (data) {
        $("#search").val(data + " Assorted Items for Sale.");
    });

})



 [HttpGet]
    public JsonResult TotalNumberofSubCateg(string SubCategory)
    {
        int rowcount;
        string constr = ConfigurationManager.ConnectionStrings["StockConnString"].ConnectionString;
        SqlConnection con = new SqlConnection(constr);
        con.Open();

        SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM DisplayCenterTab Where SubCategory = '"+ SubCategory.Replace("'","''") +"' ", con);

        cmd.CommandType = CommandType.Text;
        rowcount = Convert.ToInt32(cmd.ExecuteScalar());
        con.Close();
        return Json(rowcount, JsonRequestBehavior.AllowGet);
    }