在关键字 'and' 附近有语法错误。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 在关键字 'and' 附近有语法错误。源错误: 
行 106: */
行 107: SqlCommand cmd2=new SqlCommand(x,conn);
行 108: recNum=Convert.ToInt32(cmd2.ExecuteNonQuery());
行 109: conn.Close();
行 110: pageRecNum=Convert.ToInt32(6);
 源文件: c:\inetpub\wwwroot\box\discountsearch.aspx.cs    行: 108 
private void Page_Load(object sender, System.EventArgs e)
{
string DL_StartDate=Request.Form["DL_StartDate"];
string DL_Type=Request.Form["DL_Type"];
string txt_Key=Request.Form["txt_Key"];
string DL_EndDate=Request.Form["DL_EndDate"];
string Sql="";
if(DL_StartDate!="")
{
if(DL_StartDate=="all")
{
Sql=Sql+"a.DiscountDate<'"+DL_EndDate+"'";
}
else
{
if(DL_StartDate=="1")
{
Sql=Sql+ "a.DiscountDate between  getdate() and '"+DL_EndDate+"'";
}
                                     }
                           }
if(!Page.IsPostBack)
{

   SqlConnection conn=DBCONN.CreateCon();
conn.Open();
string x="Select count(*) from Discount a,ProductType c where a.ProductTypeID=c.ProductTypeID and "+Sql+"";
                                SqlCommand cmd2=new SqlCommand(x,conn);
recNum=Convert.ToInt32(cmd2.ExecuteNonQuery());
conn.Close();
                           }

解决方案 »

  1.   

    string x="Select count(*) from Discount a,ProductType c where a.ProductTypeID=c.ProductTypeID and "+Sql+"";
    后面的"+Sql+"";有问题???
      

  2.   

    这种问题设个断点,取到执行语句后放到SQL中执行是最好的解决方法
    建议楼主养成这个调试习惯
      

  3.   

    Sql变量有可能为空,自然sql语句就错了。
      

  4.   

    打开sqlserver事件探察器看看执行的sql语句哪里错了