我想在C#方法中传递一个参数,它将在这里接受SQL查询
我没有排队不,这是错误的。应该是这样的:我想在C#方法中传递一个参数,它将在这里接受SQL查询,c#,sql,methods,C#,Sql,Methods,我没有排队不,这是错误的。应该是这样的: public int ColumnCountinFailedQueue(long QueueNo) { string query = "select count(QueueNo) from NS_FailedQueue where queueid = @QueueNo"; string query = "select count(QueueNo) "
public int ColumnCountinFailedQueue(long QueueNo)
{
string query = "select count(QueueNo)
from NS_FailedQueue
where queueid = @QueueNo";
string query = "select count(QueueNo) "
+ "from NS_FailedQueue "
+ "where queueid = " + QueueNo.ToString();
string query = string.Format(@"select count(QueueNo) from NS_FailedQueue where queueid={0}", QueueNo);
在我看来,queueid可以是整数。。。
如果不是
queueid类似于QueueNo.tostring()我讨厌带有+的concat字符串,所以这是我的解决方案
string query = "select count(QueueNo) "
+ "from NS_FailedQueue "
+ "where queueid = " + QueueNo;
但是我认为最好使用我看到您正在尝试在这里进行参数化查询。您的问题缺少一些关键要素。如果您使用的是原始ADO.NET,则参数化查询如下所示:
string query = "select count(QueueNo) "
+ "from NS_FailedQueue "
+ "where queueid = " + QueueNo.ToString();
string query = string.Format(@"select count(QueueNo) from NS_FailedQueue where queueid={0}", QueueNo);
此外,由于您只得到一行一列,因此可以使用ExecuteScalar获得结果
应该注意的是,将参数作为字符串连接到查询末尾的其他答案也会起作用,在这种特定情况下,SQL注入不易受到攻击,因为方法参数的long将其强制为数字。问题在于,如果将此参数改为字符串,它将容易受到SQL注入的攻击