数据库服务器出现连接数过多,导致网站打不开
我也不清楚那个地方问题,最多的写法如下:
代码如下:
 //引入数据库操作类
       Admin_ClassDb classdb = new Admin_ClassDb();
        //定义所需sql语句
       string sql1 = "select top 4 id,pic,title from brandfix where Recommend='是' order by ID desc";
        //取得结果值
        DataList1.DataSource = classdb.GetTables(sql1).DefaultView;
        DataList1.DataBind();
函数:public DataTable GetTables(string sql) //返回ds.Tables["defaulttable"]
{ //从web.config中取得数据库连接字符串
string SqlConnectionStr=System.Configuration.ConfigurationSettings.AppSettings["SqlConnectionStr"];
//创建数据库连接对象
SqlConnection Sqlconn=new SqlConnection(SqlConnectionStr);
//创建DataAdapter对像
SqlDataAdapter da=new SqlDataAdapter(sql,Sqlconn);
//创建数据集
DataSet ds=new DataSet();
//填充数据集
da.Fill(ds,"defaulttable");
return ds.Tables["defaulttable"];

}这个需要释放吗

解决方案 »

  1.   

    都需要释放,下面是一个例子// This example needs the 
    // System.Data.SqlClient library
     
    #region Building the connection string
     
    string Server = "localhost";
    string Username = "my_username";
    string Password = "my_password";
    string Database = "my_database";
     
    string ConnectionString = "Data Source=" + Server + ";";
    ConnectionString += "User ID=" + Username + ";";
    ConnectionString += "Password=" + Password + ";";
    ConnectionString += "Initial Catalog=" + Database;
     
    #endregion
     
     
    #region Try to establish a connection to the database
     
    SqlConnection SQLConnection = new SqlConnection();
     
    try
    {
        SQLConnection.ConnectionString = ConnectionString;
        SQLConnection.Open();
     
        // You can get the server version 
        // SQLConnection.ServerVersion
    }
    catch (Exception Ex)
    {
        // Try to close the connection
        if (SQLConnection != null)
            SQLConnection.Dispose();
     
        // Create a (useful) error message
        string ErrorMessage = "A error occurred while trying to connect to the server.";
        ErrorMessage += Environment.NewLine;
        ErrorMessage += Environment.NewLine;
        ErrorMessage += Ex.Message;
     
        // Show error message (this = the parent Form object)
        MessageBox.Show(this, ErrorMessage, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     
        // Stop here
        return;
    }
     
    #endregion
     
     
    #region Execute a SQL query
     
    string SQLStatement = "SELECT * FROM ExampleTable";
     
    // Create a SqlDataAdapter to get the results as DataTable
    SqlDataAdapter SQLDataAdapter = new SqlDataAdapter(SQLStatement, SQLConnection);
     
    // Create a new DataTable
    DataTable dtResult = new DataTable();
     
    // Fill the DataTable with the result of the SQL statement
    SQLDataAdapter.Fill(dtResult);
     
    // Loop through all entries
    foreach (DataRow drRow in dtResult.Rows)
    {
        // Show a message box with the content of 
        // the "Name" column
        MessageBox.Show(drRow["Name"].ToString());
    }
     
    // We don't need the data adapter any more
    SQLDataAdapter.Dispose();
     
    #endregion
     
     
    #region Close the database link
     
    SQLConnection.Close();
    SQLConnection.Dispose();
     
    #endregion
      

  2.   

    你应该建立一个 静态的SqlConnection  没必要打开一个都建立一个SQLCONN对象把.
      

  3.   

    楼上的是WINFORM的,而且代码写法不一样
      

  4.   


    public static DataTable GetTables(string sql) //返回ds.Tables["defaulttable"] 
    {
    //从web.config中取得数据库连接字符串 
    string SqlConnectionStr=System.Configuration.ConfigurationSettings.AppSettings["SqlConnectionStr"]; 
    //创建数据库连接对象 
    SqlConnection Sqlconn=new SqlConnection(SqlConnectionStr); 
    //创建DataAdapter对像 
    SqlDataAdapter da=new SqlDataAdapter(sql,Sqlconn); 
    //创建数据集 
    DataSet ds=new DataSet(); 
    //填充数据集 
    da.Fill(ds,"defaulttable"); 
    Sqlconn.Close();//关闭
    return ds.Tables["defaulttable"]; 

    //自己处理异常去  不写了 
      

  5.   

    如果是web的话,静态的SqlConnection会有问题吧
      

  6.   


    是静态方法返回一个sqlconnection实例,这是楼主写的方法
    public DataTable GetTables
    ,这个方法必定属于一个非静态类,所以要实例化类,得到对象后根据对象调用这个方法。
      

  7.   

    public class Myclass
    {
    public Myclass()
    {
            connstring = ConfigurationManager.AppSettings["constr"];
    }    protected SqlConnection conn;
        protected string connstring;
        protected void Open()
        {
            if (conn == null)
            {
                conn = new SqlConnection(connstring);
            }
            if (conn.State.Equals(ConnectionState.Closed))
            {
                conn.Open();
            }
        }    public void Close()
        {
            if (conn != null)
                conn.Close();
        }
        
        public void Sql_Dml(string sql)
        {
            Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            comm.ExecuteNonQuery();
            Close();
        }    public int Sql_Dql(string sql)
        {
            Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            SqlDataReader dr = comm.ExecuteReader();
            if (dr.HasRows)
            {
                return 1;
            }
            else
            {
                return 0;
            }
            Close();        
        }
        public DataTable Sql_SelectTable(string sql)
        {
            Open();
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();
            da.Fill(ds);
            Close();
            return ds.Tables[0];        
        }
      

  8.   

    当然要及时关闭连接,
    用try catch
    加上finally来处理.数据库服务器出现连接数过多,导致网站打不开 
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    先查查数据库在执行什么语句.
      

  9.   

    1.
    这个方法本身是没有任何问题的!!!
    DataAdapter 会自动帮你管理 Connection 的 Open/Close2.
    其他方法呢?3.
    Connection 不要用 static,Connection 的开销在于 Open 操作而不在于 Connection 对象本身,本质上他应该仅仅是一个指针而已
      

  10.   

    维护 Connection 的 Open 状态才是巨大的开销,一定想用才Open,用完就Close
      

  11.   

    通过using(SqlConnection Sqlconn=new SqlConnection(SqlConnectionStr))
    {
    }
    实现,通过要关闭,最好做成S数据库操作类,可参考sqlhelp,petshop等