0

我正在尝试计算SomeColumn=SomeValue. 我的代码如下所示。它什么也没显示。

using (SqlConnection conn = new SqlConnection("ConnectionString"))
{
    conn.Open();
    string selectStatement = "Select count(*) from Jobs where 'JobCategory=cse'";

    SqlCommand cmd = new SqlCommand(selectStatement, conn);
    int count = (int)cmd.ExecuteScalar();
    Label1.Text = count.ToString();

    conn.Close();

我应该采取什么步骤?

4

4 回答 4

1

将 'JobCategory=cse' 更改为 JobCategory = 'cse'

string selectStatement = "Select count(*) from Jobs where JobCategory = 'cse'";

或尝试这样

string selectStatement = "Select count(*) from Jobs where JobCategory Like 'cse'";

在代码中实现之前,在 SQL 中检查一次查询。您是否获得预期的结果检查。

于 2013-01-18T09:20:28.467 回答
0

你的单引号在错误的地方。试试这样;

string selectStatement = "Select count(*) from Jobs where JobCategory = 'cse'";

并且请始终参数化您的 sql 查询。这可能是SQL 注入攻击的原因。在这里如何使用它;

string realcse = "Write here which value you want to filter in where clause";
string selectStatement = "Select count(*) from Jobs where JobCategory = @cse";
SqlCommand cmd = new SqlCommand(selectStatement, conn);
cmd.Parameters.AddWithValue("@cse", realcse);

在你的情况下,它应该是这样的;

string realcse = "cse";
cmd.Parameters.AddWithValue("@cse", realcse);
于 2013-01-18T09:20:49.557 回答
0
string selectStatement = "Select COUNT(*) from [Jobs] where JobCategory='cse'";
于 2013-01-18T09:23:26.353 回答
0

请使用这个

string selectStatement = "Select  COUNT(*) as cnt from [Jobs] where JobCategory='cse'";
于 2013-01-18T10:35:51.970 回答