private void Open()
        {
            try
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close                
                }
                    con.Open();
            }
            catch (Exception)
            {
                System.Web.HttpContext.Current.Response.Write("数据库连接失败");
                System.Web.HttpContext.Current.Response.End();
            }
        }
我在访问数据库的时候 经常都会出现数据库失败的错误,数据库链接我就是用的上面的函数,每次运行应该都会先关闭数据库再重新链接,怎么还会出这个错误啊?

解决方案 »

  1.   

    con.Close
    这里是什么啊?
      

  2.   

    应该是关闭方法啊
    con.Close();
      

  3.   

    不好意思,应该是这样的,
    private void Open() 
            { 
                try 
                { 
                    if (con.State == ConnectionState.Open) 
                    { 
                        con.Close()                
                    } 
                        con.Open(); 
                } 
                catch (Exception) 
                { 
                    System.Web.HttpContext.Current.Response.Write("数据库连接失败"); 
                    System.Web.HttpContext.Current.Response.End(); 
                } 
            } 循环是循环datatablepublic DataTable GetDataTable(string sqlstr)
            {
                
                SqlDataAdapter da = new SqlDataAdapter();
                DataTable datatable = new DataTable();
                    Open();
                    comm.CommandType = CommandType.Text;
                    comm.CommandText = sqlstr;
                    comm.Connection=con;
                    da.SelectCommand = comm;
                    da.Fill(datatable);            return datatable;
            }
      

  4.   


            public virtual bool Open()
            {
                if (string.IsNullOrEmpty(this._dbConnection.ConnectionString))
                {
                    throw new ArgumentNullException("ConnectionString");
                }
                if ((this._dbConnection != null) && (this._dbState == DataConnectionState.Opening))
                {
                    this.Close();
                }
                try
                {
                    this._dbConnection.Open();
                }
                catch (Exception ex)
                {
                    if (this.LogEntry == null)
                    {
                        throw ex;
                    }
                    this.LogEntry.RecordConnectionOpenError(ex);
                }
                return true;
            }
      

  5.   

    不需要打开手动打开 连接;
    DataAdapter在填充数据时会自动打开连接,取得数据后,会关闭连接。
      

  6.   

    using(sqlconntion sqlcon=new sqlconntion())
    {
       连接代码;.......
    }
    用这种一切OK
      

  7.   

            DataTable datatable = new DataTable();    
            SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);
            sda.Fill(datatable);
            return datatable;