C# 使用表中的where子句计算行数
我试图计算C# 使用表中的where子句计算行数,c#,asp.net,sql,C#,Asp.net,Sql,我试图计算SomeColumn=SomeValue所在的行数。我的代码如下所示。什么也看不出来 using (SqlConnection conn = new SqlConnection("ConnectionString")) { conn.Open(); string selectStatement = "Select count(*) from Jobs where 'JobCategory=cse'"; SqlCommand cmd = new SqlComma
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();
我应该采取什么步骤?将“JobCategory=cse”更改为“JobCategory='cse” 像 或者像这样试试
string selectStatement = "Select count(*) from Jobs where JobCategory Like 'cse'";
在SQL中检查一次查询,然后在代码中实现它。无论您是否得到了预期的结果,请检查。您的单引号位置错误。像这样尝试
string selectStatement = "Select count(*) from Jobs where JobCategory = 'cse'";
请始终参数化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);
请用这个
string selectStatement = "Select COUNT(*) as cnt from [Jobs] where JobCategory='cse'";
我确实认为用引号括起来的SQL
WHERE
条件不起作用。如果要将cse
更改为动态参数,请确保检查如何正确传递变量。请查看本网站,了解一个很好的例子:请告诉我们什么是不起作用的?你有例外吗?您是否逐行调试代码?您是否在SQL Server Management Studio中尝试了查询?结果是否正确?您的代码在我看来很好。@user1927791请告诉我们哪些代码不起作用?你有例外吗?您是否逐行调试代码?
string selectStatement = "Select COUNT(*) from [Jobs] where JobCategory='cse'";
string selectStatement = "Select COUNT(*) as cnt from [Jobs] where JobCategory='cse'";