早教吧 育儿知识 作业答案 考试题库 百科 知识分享

System.Data.SqlClient.SqlException:'a'附近有语法错误.这个是程序:publicpartialclassDiscountWare:System.Web.UI.Page{protectedvoidPageLoad(objectsender,EventArgse){ifIsPostBack){stringconnstr=ConfigurationManager.ConnectionStri

题目详情
System.Data.SqlClient.SqlException:'a' 附近有语法错误.
这个是程序:
public partial class DiscountWare :System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
if IsPostBack)
{
string connstr = ConfigurationManager.ConnectionStrings["SuperMarketConnectionString"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connstr);
sqlConn.Open();
string str = "select splb_TypeName from T_WareType";
SqlCommand cmd = new SqlCommand(str,sqlConn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList1.Items.Add(dr["splb_TypeName"].ToString());
}
GridView1.DataSourceID = "SqlDataSource1";
}
}
protected void Button1_Click(object sender,EventArgs e)
{
string connstr = ConfigurationManager.ConnectionStrings["SuperMarketConnectionString"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connstr);
sqlConn.Open();
string str = "SELECT sp_WareName,sp_AllSum,sp_SellSum,sp_Price,sp_Information,splb_TypeName";
str = str + "FROM T_Ware a JOIN T_WareType b ON a.splb_TypeID=b.splb_TypeID where";
str = str + "b.splb_TypeName=" + DropDownList1.SelectedValue.ToString().Trim() + "";
if (TextBox1.Text = "")
{
str = str + "and a.sp_WareName like" + "%" + TextBox1.Text.Trim() + "%";
}
this.GridView1.DataSourceID = null;
SqlDataSource1.SelectCommand = str;
GridView1.DataSourceID = "SqlDataSource1";
}
}
▼优质解答
答案和解析
sql字符串组合时请在结尾加空格个防止,不然会出现语法问题