日期:2014-05-17  浏览次数:20550 次

ExecuteReader 要求已打开且可用的连接 问题
之前发过:http://topic.csdn.net/u/20101022/16/d4596a4b-07fc-45f6-a2f2-fbfee071c981.html?32438

很多朋友也给了建议,但是我改来改去,问题依然存在,就是一会正常,一会不正常。求个位帮帮忙啊!!!!!

问题:

“/zhxy”应用程序中的服务器错误。
--------------------------------------------

ExecuteReader 要求已打开且可用的连接。连接的当前状态为已关闭。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.InvalidOperationException: ExecuteReader 要求已打开且可用的连接。连接的当前状态为已关闭。


OledbHelper 代码:

C# code

public class OleDbHelper
    {
        static OleDbConnection conn = null;
        static OleDbCommand cmd = null;
        static string appPath = System.Web.HttpContext.Current.Request.ApplicationPath;
        static string connStr = "provider=microsoft.jet.oledb.4.0;data source="+System.Web.HttpContext.Current.Server.MapPath(appPath + "/App_Data/ZHXYDB.mdb")+"";
        
        public OleDbConnection OpenConnection()
        {
            try
            {
                //connStr = ConfigurationManager.ConnectionStrings["DBS"].ToString() + System.Web.HttpContext.Current.Server.MapPath(appPath + "/App_Data/ZHXYDB.mdb");
                conn = new OleDbConnection(connStr);
                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
            }
            catch { }
            return conn;
        }

        public  void CloseConnection()
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }

        public void CloseConnection(OleDbConnection connection)
        {
            if (connection.State == ConnectionState.Open)
            {
                connection.Close();
            }
        }

        public object DB_ExecuteScalar(string sqlStr)
        {
            OleDbConnection conn = OpenConnection();
            cmd = new OleDbCommand(sqlStr, conn);
            return cmd.ExecuteScalar();
        }

        public OleDbDataReader ExecuteOdr(string sqlStr, OleDbConnection conn)
        {
            cmd = new OleDbCommand(sqlStr, conn);
            OleDbDataReader odr = cmd.ExecuteReader();
            return odr;
        }

        public int DB_ExecuuteNonQuery(string sqlStr)
        {
            OleDbConnection conn = OpenConnection();
            cmd = new OleDbCommand(sqlStr, conn);
            int result = cmd.ExecuteNonQuery();
            CloseConnection(conn);
            return result;
        }

        public DataTable DB_Fill(string sqlStr)
        {
            OleDbConnection conn = OpenConnection();
            DataTable dt = new DataTable();
            OleDbDataAdapter oda = new OleDbDataAdapter(sqlStr, conn);
            oda.Fill(dt);
            return dt;
        }
    }




引用 ExecuteOdr 方法的的代码:

C# code

public MODEL.CompanyInfo GetModel(int id)
        {
            MODEL.CompanyInfo model = new MODEL.CompanyInfo();
            OleDbConnection conn = helper.OpenConnection();
            sqlStr = new StringBuilder();
            sqlStr.Append("select * from CompanyInfo ");
            sqlStr.Append("where ID=" + id + "");
            OleDbDataReader odr = helper.ExecuteOdr(sqlStr.ToString (), conn);
            if (odr.Read() == true)
            {
                model.ID = (int)odr[0];
                model.InfoTitle = odr[1].ToString();
                model.InfoContent = odr[2].ToString();
            }
            odr.Close();
            helper.CloseConnection(conn);
            return model;
        }




问题