简体   繁体   English

asp.net连接打开和关闭

[英]asp.net connection open and close

Let say i have one asp.net application that is having some page that uses the connection continuesly..... 假设我有一个asp.net应用程序,该应用程序的某个页面持续使用连接.....

i have open the connection in class file in construscter ...... 我在construscter的类文件中打开了连接...

and im accessing it using the object of the class....when ever database operation are required... 并使用类的对象即时访问它。...需要进行数据库操作时...

in start the application is running fine....but after some operation with database in datagrid(ex. sorting,paging,other).... it's gets slow...and again start working after some time.... 在开始时,应用程序运行良好.....但是在datagrid中对数据库进行了某些操作(例如,排序,分页,其他)之后。它变得很慢...一段时间后又开始工作。

do you guys have any solution or any suggession for that..... 你们对此有任何解决方案或建议吗?

i have used the connection in following way.... 我以以下方式使用了连接...

public class student_operation
{
    public SqlConnection cn = new SqlConnection();
    public SqlCommand cmd = new SqlCommand();
    public SqlDataAdapter ad = new SqlDataAdapter();
    public DataSet rs = new DataSet();
    public DataSet rs1 = new DataSet();

    public student_operation()
    {
        //
        // TODO: Add constructor logic here
        //
        try
        {
            cn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString);
            cn.Open();
        }
        catch (Exception)
        {
            if (cn.State != ConnectionState.Closed)
            {
                cn.Close();
                cn.Open();
            }
        }
    }

}

Make sure you are opening AND closing your connection. 确保打开和关闭连接。 Don't worry about "pooling" the connection. 不必担心“汇集”连接。 .Net will handle that for you automatically. .Net将自动为您处理。 Just open the connection, do your work and close the connection (even if that's done in the static part). 只需打开连接,完成您的工作并关闭连接(即使这是在静态部分完成的)。

I don't see where you are closing your connection. 我看不到您要关闭连接的位置。 I would implement IDisposable and clean everything up there. 我将实现IDisposable并清理那里的所有内容。 Then you can access your class in a using statement. 然后,您可以在using语句中访问您的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM