连接数据库异常
using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace WebDAL
{
/// <summary>
/// SqlHelper 的摘要说明。
/// </summary>
public class SqlHelper
{
private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataReader sdr =null;
//SqlConnection myConnection = null;
//
/// <summary>
/// 构造SqlHelper
/// </summary>
public SqlHelper()
{
String strconn = ConfigurationSettings.AppSettings["SQLCONN"];
//String strconn = ConfigurationSettings.AppSettings["SQLCONNECTIONSTRING"];
conn= new SqlConnection(strconn);
}
private SqlConnection getConn()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
return conn;
}
执行完return conn;得到如下信息
Invalid attempt to FieldCount when reader is closed.Server Error in '/WebTest'
Application.
Invalid attempt to FieldCount when reader is closed.
Description: An unhandled exception occurred during the execution of the current
web request. Please review the stack trace for more information about the error
and where it originated in the code.
Exception Details: System.InvalidOperationException: Invalid attempt to
FieldCount when reader is closed.
Source Error:
An unhandled exception was generated during the execution of the current
web request. Information regarding the origin and location of the
exception can be identified using the exception stack trace below.
Stack Trace:
[InvalidOperationException: Invalid attempt to FieldCount when reader is closed.]
System.Data.SqlClient.SqlDataReader.get_FieldCount()
System.Data.Common.DbEnumerator.BuildSchemaInfo()
System.Data.Common.DbEnumerator.MoveNext()
System.Web.UI.WebControls.DataList.CreateControlHierarchy(Boolean useDataSource)
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
System.Web.UI.WebControls.BaseDataList.DataBind()
WebTest.Default.Page_Load(Object sender, EventArgs e) +66
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET
Version:1.1.4322.573