{
String strConnection=ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection cn=new SqlConnection(strConnection); string name1 = TextBox1.Text;
string name2 = Dp1.SelectedValue;
string strSql="select id,sysdate,name,lianxi,wentilist,wenti,youxian,haoma,ipaddress,chuli from login where 1=1 order by id desc";如果我把order by id desc去掉,查询是按顺序写的,但是我想按倒序却不行,不知咋弄,提示错误如最下面:
if(name1!="")
{
strSql+=" and name like '%"+name1+"%'";
}
if(this.Dp1.SelectedIndex>0)
{
strSql+=" and wentilist='"+name2+"'";
} SqlDataAdapter da = new SqlDataAdapter(strSql,cn);
DataSet ds=new DataSet();
da.Fill(ds,"login");
DataGrid1.DataSource=ds.Tables["login"].DefaultView;
DataGrid1.DataBind();
错误:“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------在关键字 'and' 附近有语法错误。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 在关键字 'and' 附近有语法错误。源错误: 
行 193: SqlDataAdapter da = new SqlDataAdapter(strSql,cn);
行 194: DataSet ds=new DataSet();
行 195: da.Fill(ds,"login");
行 196: DataGrid1.DataSource=ds.Tables["login"].DefaultView;
行 197: DataGrid1.DataBind();
 源文件: d:\inetpub\wwwroot\it\show1.aspx.cs    行: 195 堆栈跟踪: 
[SqlException: 在关键字 'and' 附近有语法错误。]
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
   System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
   it.show1.Button2_Click(Object sender, EventArgs e) in d:\inetpub\wwwroot\it\show1.aspx.cs:195
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain() 
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.2300; ASP.NET 版本:1.1.4322.2300 

解决方案 »

  1.   

    Desc就是倒序,Asc是顺序
    要把Order By ID Desc加在SQL语句的最后。
      

  2.   

    在 strSql+=" and name like '%"+name1+"%'";
    和strSql+=" and wentilist='"+name2+"'";
     后面加上空格试试,如果strSql+=" and name like '%"+name1+"%'  ";最好不要用动态sql,而用存储过程,将这些当参数传入!
      

  3.   

    where 1=1 order by id desc" ?
    你的那个1=1是什么,你将Where 1=1都去掉试一试!!
      

  4.   

    where 1=1 order by id desc" ?
    你的那个1=1是什么,你将Where 1=1都去掉试一试!!是全部查找
      

  5.   

    估计是你的查询语句中第一个and的前面没有条件。你可以把你拼接后的strSql打印出来看看(Copy到SQL查询分析器中执行一下就可以看到是什么错误),肯定是SQL语法错误。
      

  6.   

    问题是这样的:
    select id,sysdate,name,lianxi,wentilist,wenti,youxian,haoma,ipaddress,chuli from login where 1=1 order by id desc"  这是查"全部"内容,而且是时间顺序也是对的,但是如果点击下面的分类就会报上面的错误.但是如果不加order by id desc 不管点击"全部"还是"分类"运行都是正常的,只是点击下面的"分类"不能按倒序排例
      

  7.   

    那你把点击分类后的strSQL打印出来检查一下啊。
      

  8.   

    TO:hunanchina(重庆辣妹) 
    你保错内容和你贴出来的SQL不符。所以我才看你Where语句写的情况。
    你保错是AND用法错误。 而你写出来又没有AND。所以我让你干脆把Where去掉得了,反正都是全部返回。
    我估计你的错误是and前后有标识字符串的单引号或缺少空格。
      

  9.   

    string strSql="select id,sysdate,name,lianxi,wentilist,wenti,youxian,haoma,ipaddress,chuli from login where 1=1 ";
    if(name1!="")
    {
    strSql+=" and name like '%"+name1+"%'";
    }
    if(this.Dp1.SelectedIndex>0)
    {
    strSql+=" and wentilist='"+name2+"'";
    }
      

  10.   

    "select id,sysdate,name,lianxi,wentilist,wenti,youxian,haoma,ipaddress,chuli from login where 1=1 order by id desc"
    +
    " and name like '%"+name1+"%'"order by 要在句尾……
      

  11.   

    string strSql="select id,sysdate,name,lianxi,wentilist,wenti,youxian,haoma,ipaddress,chuli from login where 1=1 ";
    if(name1!="")
    {
    strSql+=" and name like '%"+name1+"%'";
    }
    if(this.Dp1.SelectedIndex>0)
    {
    strSql+=" and wentilist='"+name2+"'";
    }
    这样是没错,但是查出来的是按时间的顺序排列的,我想让它按时间倒序排列啊
      

  12.   

    string strSql="select id,sysdate,name,lianxi,wentilist,wenti,youxian,haoma,ipaddress,chuli from login where 1=1 ";
    if(name1!="")
    {
    strSql+=" and name like '%"+name1+"%'";
                                         strSql+="order by id desc ";
    }
    if(this.Dp1.SelectedIndex>0)
    {
    strSql+=" and wentilist='"+name2+"'";
                                         strSql+="order by id desc ";
    }
      

  13.   

    string strSql="select id,sysdate,name,lianxi,wentilist,wenti,youxian,haoma,ipaddress,chuli from login where 1=1 ";
    if(name1!="")
    {
    strSql+=" and name like '%"+name1+"%'";
    }
    if(this.Dp1.SelectedIndex>0)
    {
    strSql+=" and wentilist='"+name2+"'";
    }
    strSql+=" order by id desc ";