问题描述
stringConStr2=ConfigurationManager.ConnectionStrings["SperConnectionString1"].ToString();SqlConnectionconn2=newSqlConnection(ConStr2);conn2.Open();stringsqlma="";if(Request.QueryString["key"]==hits.Text){sqlma="select*fromnewsorderbyhitsdesc";}if(Request.QueryString["key"]==dtt.Text){sqlma="select*fromnewsorderbydttdesc";}SqlDataAdaptersqlda=newSqlDataAdapter(sqlma,conn2);DataSetds=newDataSet();sqlda.Fill(ds);我本来想根据前页传递的值来排序,怎么会出这个问题呢,
解决方案
本帖最后由 wrrlpx1234 于 2014-09-14 17:06:04 编辑
解决方案二:
没有对sqlcommand对象的commandtext属性赋值SqlCommandcommand=newSqlCommand(sqlma,conn2);SqlDataAdapteradapter=newSqlDataAdapter();adapter.SelectCommand=command;DataSetds=newDataSet();sqlda.Fill(ds);
解决方案三:
解决方案四:
stringkid=Request.QueryString["key"];stringConStr2=ConfigurationManager.ConnectionStrings["SperConnectionString1"].ToString();SqlConnectionconn2=newSqlConnection(ConStr2);conn2.Open();stringsql3="select*fromnewsorderby";stringorderby="";switch("kid"){case"1":orderby="hitsdesc";break;case"2":orderby="dttdesc";break;}sql3+=orderby;SqlDataAdaptersqlda=newSqlDataAdapter(sql3,conn2);DataSetds=newDataSet();
现在错误变成异常详细信息:System.Data.SqlClient.SqlException:'by'附近有语法错误。源错误:行69:SqlDataAdaptersqlda=newSqlDataAdapter(sql3,conn2);行70:DataSetds=newDataSet();行71:sqlda.Fill(ds);
解决方案五:
引用3楼wrrlpx1234的回复:
stringkid=Request.QueryString["key"];stringConStr2=ConfigurationManager.ConnectionStrings["SperConnectionString1"].ToString();SqlConnectionconn2=newSqlConnection(ConStr2);conn2.Open();stringsql3="select*fromnewsorderby";stringorderby="";switch("kid"){case"1":orderby="hitsdesc";break;case"2":orderby="dttdesc";break;}sql3+=orderby;SqlDataAdaptersqlda=newSqlDataAdapter(sql3,conn2);DataSetds=newDataSet();现在错误变成异常详细信息:System.Data.SqlClient.SqlException:'by'附近有语法错误。源错误:行69:SqlDataAdaptersqlda=newSqlDataAdapter(sql3,conn2);行70:DataSetds=newDataSet();行71:sqlda.Fill(ds);
switch("kid"){case"1":orderby="hitsdesc";break;case"2":orderby="dttdesc";break;default:orderby="xxxdesc";break;}两个前面都加个空格,话说,最好有个默认的,不然switch没有匹配也会出错的