我试图计算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();
我应该采取什么步骤?
答案 0 :(得分: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中检查一次查询,然后在代码中实现它。是否获得预期结果。
答案 1 :(得分:0)
您的单引号位置错误。试试这个;
string selectStatement = "Select count(*) from Jobs where JobCategory = 'cse'";
请始终参数化您的SQL查询。这可能是SQL Injection攻击的原因。在这里你可以如何使用它;
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);
答案 2 :(得分:0)
string selectStatement = "Select COUNT(*) from [Jobs] where JobCategory='cse'";
答案 3 :(得分:0)
请使用此
string selectStatement = "Select COUNT(*) as cnt from [Jobs] where JobCategory='cse'";