连接数据库异常

william51 2009-03-30 07:05:23

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
...全文
41 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧