参数化会话和查询字符串
本文关键字:查询 字符串 会话 参数 | 更新日期: 2023-09-27 18:11:08
我正面临一个错误消息,我不知道原因与我的sql语句在行:
DACatPgeVIPLIST.Fill(dsCatPgeVIPLIST);
显示了这个信息,你能帮我一下吗?
代码:参数化查询
(@Country nvarchar(7),@Category nvarchar(4000))SELECT a.[AdsID],
需要参数@Category
,但没有提供。
if (Session["location"] != null)
{
using (SqlConnection CatPgeVIPLISTsqlCON = new SqlConnection(cs))
{
CatPgeVIPLISTsqlCON.Open();
SqlDataAdapter DACatPgeVIPLIST = new SqlDataAdapter("SELECT a.[AdsID], a.[Country], a.[State], a.[City], a.[AdsTit], SUBSTRING(a.[AdsDesc], 1, 70) as AdsDesc, a.[AdsPrice], a.[Img1] FROM [ads] as a INNER JOIN [UserInfo] as u on u.UID = a.UID WHERE a.[Country] = @Country and a.[Category] = @Category and u.VIP = 'Yes'", cs);
string location = Convert.ToString(Session["location"]);
string category = Request.QueryString["category"];
DACatPgeVIPLIST.SelectCommand.Parameters.AddWithValue("@Country", location);
DACatPgeVIPLIST.SelectCommand.Parameters.AddWithValue("@Category", category);
DataSet dsCatPgeVIPLIST = new DataSet();
DACatPgeVIPLIST.Fill(dsCatPgeVIPLIST);
CatPgeVIPLIST.DataSource = dsCatPgeVIPLIST.Tables[0];
CatPgeVIPLIST.DataBind();
}
}
下面一行代码可以将null
分配到category:
string category = Request.QueryString["category"];
您可以像这样绕过它,将null
转换为空字符串:
string category = Convert.ToString(Request.QueryString["category"]);
或者您可以尝试传递DBNull.Value
而不是null
(未测试):
DACatPgeVIPLIST.SelectCommand.Parameters
.AddWithValue("@Category", (object)category ?? DBNull.Value);