System.InvalidOperationException: ExecuteReader: CommandText 属性尚未初始化

问题描述

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没有匹配也会出错的

时间: 2024-09-20 19:02:00

System.InvalidOperationException: ExecuteReader: CommandText 属性尚未初始化的相关文章

c#winform-System.InvalidOperationException: ExecuteReader: Connection 属性尚未初始化。

问题描述 System.InvalidOperationException: ExecuteReader: Connection 属性尚未初始化. 有关调用实时(JIT)调试而不是此对话框的详细信息,请参见此消息的结尾. ************** 异常文本 **************System.InvalidOperationException: ExecuteReader: Connection 属性尚未初始化. 在 System.Data.SqlServerCe.SqlCeComma

求解 为什么运行一下代码时总是出现“ExecuteReader: CommandText 属性尚未初始化”这样的问题,请问是什么原因,求大神解答?

问题描述 usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Data;usingSystem.Data.SqlClient;publicpartialclass_Default:System.Web.UI.Page{protectedvoidbtnLogin_Click(

ExecuteReader: CommandText 属性尚未初始化

问题描述 ///<summary>///说明:GetDataSet数据集,返回数据源的数据集///</summary>publicSystem.Data.DataSetGetDataSet(stringsQueryString,stringTableName){try{SqlDataAdapteradapter=newSqlDataAdapter(sQueryString,conn);SqlCommandBuilderbuider=newSqlCommandBuilder(adap

asp.net-总是报ExecuteReader: Connection 属性尚未初始化。属性尚未初始化。”是怎么回事啊

问题描述 总是报ExecuteReader: Connection 属性尚未初始化.属性尚未初始化."是怎么回事啊 using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.

sqlserver-关于Fill: SelectCommand.Connection 属性尚未初始化

问题描述 关于Fill: SelectCommand.Connection 属性尚未初始化 string cmd = "select * from mydb"; SqlCommand sql = new SqlCommand(cmd); SqlConnection conn = new SqlConnection(@"Data Source=Localhost;Database=company;Integrated Security=True"); try { co

ConnectionString 属性尚未初始化

问题描述 publicDatabase(){////TODO:在此处添加构造函数逻辑//ConnectionString=ConfigurationManager.AppSettings["DBConnectionString"];}protectedvoidOpen(){if(Connection==null){Connection=newSqlConnection(ConnectionString);}if(Connection.State.Equals(ConnectionSta

.net设计登录页面,点击登录按钮,一直提示“ConnectionString 属性尚未初始化。”

问题描述 以下为数据库链连接代码:usingSystem;usingSystem.Data.SqlClient;usingSystem.Data;usingSystem.Configuration;namespaceGROUP.Manage{///<summary>///BaseClass的摘要说明.///</summary>publicclassBaseClass:System.Web.UI.Page{StringConnectionString;publicBaseClass(

connectionstring-DataAdapter ConnectionString属性尚未初始化

问题描述 DataAdapter ConnectionString属性尚未初始化 private DataSet ds = new DataSet(); DataTable dt = new DataTable(); private SqlDataAdapter dap = new SqlDataAdapter(); BindingSource bs = new BindingSource(); 这是我调用数据库数据的方法: public DataSet AccessDBData(string

SelectCommand.Connection 属性尚未初始化 在线急求

问题描述 SqlConnectionmyconn=newSqlConnection();myconn.ConnectionString=ConfigurationManager.ConnectionStrings["db_NetShopConnectionString1"].ConnectionString;myconn.Open();SqlCommandcmd=newSqlCommand();stringP_Str_SqlStr="select*fromtb_Class&q