当前位置:编程学习 > C#/ASP.NET >>

网站经常出现以下错误,导致网站经常打不开,不知道为什么,请大家指点?

我网站在PageBase.cs里面连接的数据库,这样要方便很多,不用每个页面都去连接数据库,只需要调用基类的就可以,但时不时的会出现下面的这种情况,导致网站无法访问,请大家指点小弟,该如何操作。


//打开数据库
        private void Open()
        {
            if (this.OledbConn == null)
                this.OledbConn = new OleDbConnection(this.OledbStr);
            if (this.OledbConn.State == ConnectionState.Closed)
                this.OledbConn.Open();
        }

        //关闭数据库

        private void Close()
        {
            if (this.OledbConn.State == ConnectionState.Open)
                this.OledbConn.Close();
            if (this.OledbConn != null)
                this.OledbConn.Dispose();
            this.OledbConn = null;
        }
--------------------编程问答-------------------- 为了方便大家帮我解答,我把PageBase.cs的代码全部粘贴出来:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.OleDb;

/// <summary>
///PageBase 的摘要说明
/// </summary>
namespace didian
{
    /// <summary>
    /// PageBase 的摘要说明
    /// </summary>
    public class PageBase : System.Web.UI.Page
    {
        private string OledbStr;
        private OleDbConnection OledbConn;

        public PageBase()
        {
            this.OledbStr = 数据库链接字符串;
        }
        public PageBase(string connstr)
        {
            this.OledbStr = connstr;
        }

        //打开数据库
        private void Open()
        {
            if (this.OledbConn == null)
                this.OledbConn = new OleDbConnection(this.OledbStr);
            if (this.OledbConn.State == ConnectionState.Closed)
                this.OledbConn.Open();
        }

        //关闭数据库

        private void Close()
        {
            if (this.OledbConn.State == ConnectionState.Open)
                this.OledbConn.Close();
            if (this.OledbConn != null)
                this.OledbConn.Dispose();
            this.OledbConn = null;
        }

        public void datab(string sql, ref DataSet ds)
        {
            this.Open();
            //OleDbCommand comm = new OleDbCommand(sql,this.OledbConn);
            OleDbDataAdapter adap = new OleDbDataAdapter(sql, this.OledbConn);
            adap.Fill(ds);
            this.Close();
            return;
        }

        public void datab1(string sql, ref DataSet ds)
        {
            this.Open();
            //OleDbCommand comm = new OleDbCommand(sql,this.OledbConn);
            OleDbDataAdapter adap = new OleDbDataAdapter(sql, this.OledbConn);
            adap.Fill(ds);
            this.Close();
            return;
        }
        public void InsertRecord(string sql)
        {
            OleDbConnection IRConn = new OleDbConnection(this.OledbStr);
            OleDbCommand IRComm = new OleDbCommand(sql, IRConn);
            //OleDbDataReader datareader;
            IRComm.Connection.Open();
            //datareader = IRComm.ExecuteNonQuery();
            IRComm.ExecuteNonQuery();
            IRComm.Connection.Close();
        }


        public OleDbDataReader readrow(string sqlstr1)
        {
            OleDbConnection datareaderconn = new OleDbConnection(this.OledbStr);
            OleDbCommand datareadercomm = new OleDbCommand(sqlstr1, datareaderconn);
            OleDbDataReader datareader;
            datareadercomm.Connection.Open();
            datareader = datareadercomm.ExecuteReader();

            if (datareader.Read())
            {
                datareadercomm.Dispose();
                return datareader;
            }
            else
            {
                datareadercomm.Dispose();
                return null;
            }
            datareadercomm.Connection.Close();
        }


        public int CalculateRecord()
        {
            int intCount;
            string strCount = "select count(*) as co from works"; //根据条件查询有多少个记录元素
            //OleDbConnection Myconn = new OleDbConnection(this.OledbStr);
            OleDbCommand MyComm = new OleDbCommand(strCount, this.OledbConn);
            OleDbDataReader dr = MyComm.ExecuteReader();
            if (dr.Read())
            {
                intCount = Int32.Parse(dr["co"].ToString());
            }
            else
            {
                intCount = 0;
            }
            dr.Close();
            return intCount;
        }


        public void CreateSource(string sql, ref DataSet ds, int PageSize, int CurrentPage)
        {
            this.Open();

            int StartIndex;

            //设定导入的起终地址
            StartIndex = CurrentPage * PageSize;

            OleDbDataAdapter MyAdapter = new OleDbDataAdapter(sql, this.OledbConn);
            MyAdapter.Fill(ds, StartIndex, PageSize, "Score");

            return;
            this.Close();
        }

        public DataSet returnDs(string sqlStr)
        {
            this.Open();
            DataSet ds = new DataSet();
            OleDbDataAdapter odp = new OleDbDataAdapter(sqlStr, this.OledbConn);
            odp.Fill(ds, "temptable");
            return ds;
            this.Close();
        }


        public OleDbDataReader Check(string sql)
        {
            OleDbConnection clconn = new OleDbConnection(this.OledbStr);
            OleDbCommand clcomm = new OleDbCommand(sql, clconn);
            clcomm.Connection.Open();
            OleDbDataReader dr = clcomm.ExecuteReader();

            return dr;
            clcomm.Connection.Close();
        }
public string cutString(string strInput, int intLen)
        {
            strInput = strInput.Trim();
            byte[] myByte = System.Text.Encoding.Default.GetBytes(strInput);
            if (myByte.Length > intLen)
            {
                //截取操作
                string resultStr = "";
                for (int i = 0; i < strInput.Length; i++)
                {
                    byte[] tempByte = System.Text.Encoding.Default.GetBytes(resultStr);
                    if (tempByte.Length < intLen)
                    {

                        resultStr += strInput.Substring(i, 1);
                    }
                    else
                    {
                        break;
                    }
                }
                return resultStr + "...";
            }
            else
            {
                return strInput;
            }
        }
    }
}


请大家帮帮我,谢谢,高分重谢。 --------------------编程问答-------------------- 这问题不好说,我一般用官方的操作类,免得出问题!可能是连接打开了没关闭 , --------------------编程问答-------------------- 设置个断点  一步步调试 --------------------编程问答-------------------- 你把那步判断状态的删掉  直接打开连接看看呢
我一般都这么写的
public static SqlConnection Cnn
        {
            get
            {
                _cnnstring = ConfigurationManager.AppSettings["ConnectionString"].ToString();
                _cnn = new SqlConnection();
                try
                {
                    _cnn.ConnectionString = _cnnstring;
                    _cnn.Open();
                }
                catch
                {
                }
                return _cnn;
            }
        }
        public static void Close()
        {
            try
            {
                _cnn.Close();
            }
            catch
            { }
        }


--------------------编程问答--------------------
引用 4 楼 sglcj 的回复:
你把那步判断状态的删掉  直接打开连接看看呢
我一般都这么写的
C# codepublicstatic SqlConnection Cnn
        {get
            {
                _cnnstring= ConfigurationManager.AppSettings["ConnectionString"].ToString();
                _cnn=new SqlConnection();try
                {
                    _cnn.ConnectionString= _cnnstring;
                    _cnn.Open();
                }catch
                {
                }return _cnn;
            }
        }publicstaticvoid Close()
        {try
            {
                _cnn.Close();
            }catch
            { }
        }
下划线开头的看的头晕 --------------------编程问答-------------------- 建议按SQLHELPER的方法,不要建立类里的connection,在函数里建 --------------------编程问答-------------------- 你是什么数据库?
Access?
好像Access对打开的连接数有限制 --------------------编程问答-------------------- 用SQLHelper吧 --------------------编程问答-------------------- OleDbDataAdapter不需要打开显式连接
public DataSet returnDs(string sqlStr)
        {
            this.Open();
            DataSet ds = new DataSet();
            OleDbDataAdapter odp = new OleDbDataAdapter(sqlStr, this.OledbConn);
            odp.Fill(ds, "temptable");
            return ds;
            this.Close();
        }



可以改成下面这样的

public DataSet returnDs(string sqlStr)
        {
            DataSet ds = new DataSet();
            OleDbDataAdapter odp = new OleDbDataAdapter(sqlStr, this.OledbConn);
            odp.Fill(ds, "temptable");
            return ds;
        }

再说,你都return ds;了,后面的this.Close();是不会被执行的,你最好捕捉一下错误,看一下错误原因的说明 --------------------编程问答--------------------

        private void Open()
        {
            if (this.OledbConn == null)
                this.OledbConn = new OleDbConnection(this.OledbStr);
            if (this.OledbConn.State == ConnectionState.Closed)
                this.OledbConn.Open();
        }

如果单纯按逻辑讲,你这里需要改一下结构吧,楼主试一下

        private void Open()
        {
            if (this.OledbConn == null){
                this.OledbConn = new OleDbConnection(this.OledbStr);
            }
            else if(this.OledbConn.State == ConnectionState.Closed)
            {
                this.OledbConn.Open();
            }
        }
--------------------编程问答-------------------- 关闭的类似上面的改一下 --------------------编程问答-------------------- 看到头昏

应该是数据库超出最大连接数

你看看什么地方的conn 没有 close --------------------编程问答-------------------- 这样每个页面都继承基类,打开数据库,要是不关闭,容易造成这样的结果。

我觉得,数据库操作写到基类,每个页面都继承,这样不好。还是哪里用,哪里引用sqlhelper,随用随关闭

再说Microsoft给你写好了sqlhelper,何必自己费劲呢。 --------------------编程问答-------------------- 数据库的操作类,可以使用静态的函数,使用也很方便
做到随用随关的原则,特别是ACCESS数据库。看楼主的代码,没有严格的去做关闭的代码
使用USING或FINALLY都是可以的。
给楼主纠正下:
1)没有良好的注释习惯;
2)没有严格写数据库关闭代码;
3)画蛇添足的地方比较多,比如不返回参数的函数,最后没必要再写个RETURN;
4)string strCount = "select count(*) as co from works";类似这种的,可以使用另外一个方法,没有必要DataReader;
5)if判断要返回参数的,在函数体内要注意逻辑,否则条件一多,你久疯了,通常的做法就是哪里判断哪里RETURN出去,无需ELSE出无用的信息,放到最后一次RETURN就可以;
6)楼主应该下个微软的PETSHOP来看看,参考里面的SQLHELP来看看。、
…暂时写这么多 --------------------编程问答--------------------
楼主能确保连接字符串无误? --------------------编程问答-------------------- 估计是有没Close的吧。你在Open和Close两个方法里面做个计数器,保存到Applicaiton[]里面,看看是不是Open多Close少。 --------------------编程问答-------------------- 数据库连接没有很好的释放..所以僦出这种问题..

仔细检查..有哪里没有close() --------------------编程问答-------------------- 同意5楼的 --------------------编程问答-------------------- o --------------------编程问答-------------------- 连接的字符串为什么没有上传上来呢:Access数据库是基于文件型的!不知道是不是你的连接字符串有没有问题,看不见,代码没有什么问题的啦,有自己的创意!很好,顶一下》》》》海纳百川》》》》
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,