【发布时间】:2011-04-16 04:04:19
【问题描述】:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection("Data Source=DELL-PC\\SQLEXPRESS;Initial Catalog=eclass;Persist Security Info=True;integrated security = true");
myConnection.Open();
string key = txtsearchkey.Text.ToString();
SqlCommand q1 = new SqlCommand("select cat_id from category where cat_name='" + (ddsearchcat.SelectedItem.ToString() + "'"), myConnection);
string cat = q1.ExecuteScalar().ToString();
SqlCommand q2 = new SqlCommand("select subcat_id from subcategory where subcat_name= '" + (ddsearchsubcat.SelectedItem.ToString() + "'"), myConnection);
string subcat = q2.ExecuteScalar().ToString();
SqlCommand q3 = new SqlCommand("select adid from adType where adtype= '" + (ddsearchtype.SelectedItem.ToString()) + "'", myConnection);
string adtype = q3.ExecuteScalar().ToString();
String date = ddsearchdays.SelectedItem.ToString();
if (chkAdimg.Checked)
{
if (chkAdVideo.Checked)
{
SqlCommand query = new SqlCommand("select title,ad_description from postad where ad_description like " + txtsearchkey + " and category_id=" + cat + " and subcategory_id=" + subcat + " and ad_id=" + adtype + " and video is not null and img_id is not null and adType INNER JOIN adType AS adType_1 ON adType.adid = adType_1.adid CROSS JOIN category CROSS JOIN subcategory CROSS JOIN userdetails", myConnection);
DataSet ds = new DataSet();
SqlDataAdapter ad = new SqlDataAdapter(query);
ad.Fill(ds);
foreach (DataRow dr in ds.Tables[0].Rows)
{
Response.Write(dr[0].ToString());
}
}
}
}
这个查询给了我一个问题
非布尔类型的表达式 在上下文中指定 条件是预期的,靠近'INNER...
我应该对查询进行哪些更改
【问题讨论】:
-
看起来你的
SqlCommand query行可能是罪魁祸首。我的猜测是您上面的查询之一,您使用值来构建您的SqlCommand query正在返回一个无效的值。您应该逐步检查以确保获得正确的值,并且可能应该在使用它们构建另一个查询之前验证这些变量 -
首先:不将你的 SQL 查询串起来!你知道SQL injection 吗?不要那样做——永远不要。请改用参数化查询!
标签: c# sql-server-2005