如何保护我的查询:我得到 The SqlParameter is already contained by another SqlParameterCollection
How to secure my query: I get The SqlParameter is already contained by another SqlParameterCollection
我正在尝试参数化所有查询以满足 SQL Visual Studio 中 PUMA 漏洞扫描扩展的注入结果。这样做时,我 运行 陷入了标题中的错误:
The SqlParameter is already contained by another SqlParameterCollection
这是相关代码(第二行产生错误):
IEnumerable<UploadedDocsClass> docLst = prfDao.GetUploadedDocLog(Convert.ToInt32(id));
if (docLst.Count() > 0){ //<-- this is where the error is thrown
...
}
public IEnumerable<UploadedDocsClass> GetUploadedDocLog(int tickId)
{
string s = "SELECT * FROM MyTable WHERE [request_id] = @tickId ";
MyDataBaseContext accCon = new MyDataBaseContext();
return accCon.Database.SqlQuery<UploadedDocsClass>(s, new SqlParameter("@tickId", Convert.ToString(tickId)));
}
我已经在此处和 Google 中进行了搜索,但似乎找不到适合我的具体情况的解决方案。我试过克隆参数,专门声明它并在使用后将其设置为空,在使用它之前将其放入 collection 中,我什至尝试在参数名称末尾添加一个随机数所以它是每次都不同,但没什么,同样的错误。
如何在查询中使用参数的同时解决这个问题?
谢谢
因为IEnumerable<UploadedDocsClass>
返回了。它在 docLst.Count()
调用时尝试再次执行 - 不同的执行
尝试返回 List<T>
MSDN: The query is not executed when this object is created; it is executed each time it is enumerated, for example by using foreach.
显然,它正在尝试重用添加到另一个集合(先前执行)的参数
我正在尝试参数化所有查询以满足 SQL Visual Studio 中 PUMA 漏洞扫描扩展的注入结果。这样做时,我 运行 陷入了标题中的错误:
The SqlParameter is already contained by another SqlParameterCollection
这是相关代码(第二行产生错误):
IEnumerable<UploadedDocsClass> docLst = prfDao.GetUploadedDocLog(Convert.ToInt32(id));
if (docLst.Count() > 0){ //<-- this is where the error is thrown
...
}
public IEnumerable<UploadedDocsClass> GetUploadedDocLog(int tickId)
{
string s = "SELECT * FROM MyTable WHERE [request_id] = @tickId ";
MyDataBaseContext accCon = new MyDataBaseContext();
return accCon.Database.SqlQuery<UploadedDocsClass>(s, new SqlParameter("@tickId", Convert.ToString(tickId)));
}
我已经在此处和 Google 中进行了搜索,但似乎找不到适合我的具体情况的解决方案。我试过克隆参数,专门声明它并在使用后将其设置为空,在使用它之前将其放入 collection 中,我什至尝试在参数名称末尾添加一个随机数所以它是每次都不同,但没什么,同样的错误。
如何在查询中使用参数的同时解决这个问题?
谢谢
因为IEnumerable<UploadedDocsClass>
返回了。它在 docLst.Count()
调用时尝试再次执行 - 不同的执行
尝试返回 List<T>
MSDN: The query is not executed when this object is created; it is executed each time it is enumerated, for example by using foreach.
显然,它正在尝试重用添加到另一个集合(先前执行)的参数