问题描述
usingSystem;usingSystem.Text;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Configuration;namespaceWebDAL{///<summary>///SqlHelper的摘要说明。///</summary>publicclassSqlHelper{privateSqlConnectionconn=null;privateSqlCommandcmd=null;privateSqlDataReadersdr=null;//SqlConnectionmyConnection=null;/////<summary>///构造SqlHelper///</summary>publicSqlHelper(){Stringstrconn=ConfigurationSettings.AppSettings["SQLCONN"];//Stringstrconn=ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"];conn=newSqlConnection(strconn);}privateSqlConnectiongetConn(){if(conn.State==ConnectionState.Closed){conn.Open();}returnconn;}执行完returnconn;得到如下信息InvalidattempttoFieldCountwhenreaderisclosed.ServerErrorin'/WebTest'Application.InvalidattempttoFieldCountwhenreaderisclosed.Description:Anunhandledexceptionoccurredduringtheexecutionofthecurrentwebrequest.Pleasereviewthestacktraceformoreinformationabouttheerrorandwhereitoriginatedinthecode.ExceptionDetails:System.InvalidOperationException:InvalidattempttoFieldCountwhenreaderisclosed.SourceError:Anunhandledexceptionwasgeneratedduringtheexecutionofthecurrentwebrequest.Informationregardingtheoriginandlocationoftheexceptioncanbeidentifiedusingtheexceptionstacktracebelow.StackTrace:[InvalidOperationException:InvalidattempttoFieldCountwhenreaderisclosed.]System.Data.SqlClient.SqlDataReader.get_FieldCount()System.Data.Common.DbEnumerator.BuildSchemaInfo()System.Data.Common.DbEnumerator.MoveNext()System.Web.UI.WebControls.DataList.CreateControlHierarchy(BooleanuseDataSource)System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgse)System.Web.UI.WebControls.BaseDataList.DataBind()WebTest.Default.Page_Load(Objectsender,EventArgse)+66System.Web.UI.Control.OnLoad(EventArgse)System.Web.UI.Control.LoadRecursive()System.Web.UI.Page.ProcessRequestMain()VersionInformation:Microsoft.NETFrameworkVersion:1.1.4322.573;ASP.NETVersion:1.1.4322.573