回 帖 发 新 帖 刷新版面

主题:关于SQL搜索

SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            con.Open();
            string searchProductTypeName = Convert.ToString(Session["searchProductTypeName"]);
            string searchProductTypeID = Convert.ToString(Session["searchProductTypeID"]);
            string searchKey = Convert.ToString(Session["searchKey"]);
            string searchLowPrice = Convert.ToString(Session["searchLowPrice"]);
            string searchHighPrice = Convert.ToString(Session["searchHighPrice"]);

            //Response.Write(searchProductTypeName);
            //Response.Write(searchProductTypeID);
            //Response.Write(searchKey);
            //Response.Write(searchLowPrice);
            //Response.Write(searchHighPrice);

            string sqlstrProduct = "select productName from T_Product_Info where productTypeName='" + searchProductTypeName + "'";
            SqlDataAdapter sdaProduct = new SqlDataAdapter(sqlstrProduct, con);
            DataSet dsProduct = new DataSet();
            sdaProduct.Fill(dsProduct, "T_Product_Info");
            DataRowView drvProduct = dsProduct.Tables["T_Product_Info"].DefaultView[0];
            string productName = Convert.ToString(drvProduct.Row["productName"]);
            string sqlstrSearch = "select * from vb_ProductInfo where productTypeID='" + searchProductTypeID
                + "' and " + productName + " like '%" + searchKey + "%'";
            
            SqlDataAdapter sdaSearch = new SqlDataAdapter(sqlstrSearch, con);
            DataSet dsSearch = new DataSet();
            sdaSearch.Fill(dsSearch);
            GVProductSearch.DataSource = dsSearch;
            GVProductSearch.DataBind();
            con.Close();
        }
    }

以上是我做的商品搜索条的后台。
但是在搜索的SQL语句上碰到的问题。
string sqlstrSearch = "select * from vb_ProductInfo where productTypeID='" + searchProductTypeID
                + "' and " + productName + " like '%" + searchKey + "%'";
我希望的是在关键字里输入关于商品名称搜索的内容,然后拿到DDL所选择的商品种类里进行选择,比如我输入的关键字是诺基亚,但是就会报错说“诺基亚的列不存在”

这个搜索语句到底应该怎么写呢?

回复列表 (共1个回复)

沙发

将此语句输出看看取得的值是什么?

我来回复

您尚未登录,请登录后再回复。点此登录或注册