当应用程序运行时,我收到错误或冻结,因为它给了我消息:
Must declare the scalar variable "@Category" " next to dashowadscat.Fill(dsadsshowcat);
。
在QueryString [" cat"]
下还有一个红线 using (SqlConnection shwadscat = new SqlConnection(cs))
{
shwadscat.Open();
SqlDataAdapter dashowadscat =
new SqlDataAdapter("SELECT DISTINCT [AdsID],
[Section],
[Category],
[Country],
[State],
[City],
[AdsTit],
SUBSTRING([AdsDesc],1,155) as AdsDesc,
[AdsPrice],
[Img1]
FROM [ads]
WHERE (([Category] = @Category)
AND ([Country] = @Country))", cs);
string location = Convert.ToString(Session["location"]);
string cat = Convert.ToString(QueryString["cat"]);
DataSet dsadsshowcat = new DataSet();
dashowadscat.Fill(dsadsshowcat);
cateshowlistview.DataSource = dsadsshowcat.Tables[0];
cateshowlistview.DataBind();
}
}
else
{...}
答案 0 :(得分:0)
select语句在where子句中需要2个参数,而你没有提供它们。
答案 1 :(得分:0)
请检查您的SQL SELECT DISTINCT [AdsID],[Section], [Category], [Country], [State], [City], [AdsTit], SUBSTRING([AdsDesc],1,155) as AdsDesc, [AdsPrice], [Img1] FROM [ads] WHERE (([Category] = @Category) AND ([Country] = @Country))
我相信您希望传递@Category
和@Country
作为参数,您可以检查SqlParameter
,否则,您可以在以下代码中连接SQL:
修改(使用整个代码展开代码,并将cs
更改为shwadscat
:
using (SqlConnection shwadscat = new SqlConnection(cs))
{
shwadscat.Open();
string location = Convert.ToString(Session["location"]);
string cat = Convert.ToString(QueryString["cat"]);
SqlDataAdapter dashowadscat = new SqlDataAdapter("SELECT DISTINCT [AdsID],[Section], [Category], [Country], [State], [City], [AdsTit], SUBSTRING([AdsDesc],1,155) as AdsDesc, [AdsPrice], [Img1] FROM [ads] WHERE (([Category] = '" + cat+ "') AND ([Country] = '" + location + "'))", shwadscat);
DataSet dsadsshowcat = new DataSet();
dashowadscat.Fill(dsadsshowcat);
cateshowlistview.DataSource = dsadsshowcat.Tables[0];
cateshowlistview.DataBind();
}
答案 2 :(得分:0)
您尚未声明标量变量@Category
,这是一个示例:
SqlCommand cmd = new SqlCommand("SELECT DISTINCT [AdsID],[Section], [Category], [Country], [State], [City], [AdsTit], SUBSTRING([AdsDesc],1,155) as AdsDesc, [AdsPrice], [Img1] FROM [ads] WHERE (([Category] = @Category) AND ([Country] = @Country))", cs);
cmd.Parameters.Add("@Category", SqlDbType.Int);
cmd.Parameters["@ID"].Value = "";
cmd.Parameters.Add("@Country", SqlDbType.Int);
cmd.Parameters["@ID"].Value = "";
答案 3 :(得分:0)
一些问题......
您需要在查询字符串中指定参数的值。
我假设您需要Request.QueryString
,而不仅仅是QueryString
,这本身并不代表任何内容,这就是您收到语法错误的原因。
您当前正在传递SqlDataAdapter
连接字符串,而不是您实例化的SqlConnection
。如果您的连接字符串适用于SqlDataAdapter
,那么您可以完全删除SqlConnection
" shwadscat"。
试试这个:
var location = Convert.ToString(Session["location"]);
var cat = Convert.ToString(Request.QueryString["cat"]);
var dsadsshowcat = new DataSet();
using (SqlConnection shwadscat = new SqlConnection(cs))
{
shwadscat.Open();
using (var dashowadscat = new SqlDataAdapter("SELECT DISTINCT AdsID, Section, Category, Country, State, City, AdsTit, SUBSTRING(AdsDesc,1,155) as AdsDesc, AdsPrice, Img1 FROM ads WHERE Category = @Category AND Country = @Country", shwadscat))
{
dashowadscat.SelectCommand.Parameters.AddWithValue("@Category", cat);
dashowadscat.SelectCommand.Parameters.AddWithValue("@Country", location);
dashowadscat.Fill(dsadsshowcat);
cateshowlistview.DataSource = dsadsshowcat.Tables[0];
cateshowlistview.DataBind();
}
}
通过方式参数化您的查询的荣誉。这是更安全,更易于维护的方式。