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());
}
}
}
}
這個查詢是給了我一個問題,說sql查詢問題
其中 條件,預計在指定的上下文非布爾型 的表達,接近內心...
我應該讓我的查詢什麼變化
看起來你`的SqlCommand query`線可能是罪魁禍首。我的猜測是你上面的查詢之一,你使用值來建立你的`SqlCommand查詢`返回一個無效的值。你應該通過它來確保你獲得了正確的值,並且可能應該在使用它們來構建另一個查詢之前驗證這些變量。 – Prescott 2011-04-15 16:13:59
**首先:**將** NOT **字符串連接到SQL查詢中!你知道嗎[SQL注入](http://xkcd.com/327/)?不要這樣做 - 不是永遠。請改用**參數化查詢**! – 2011-04-15 20:02:23