簡體   English   中英

在C#.net中單擊取消時如何關閉所有數據庫連接

[英]How to close all db connection when clicked cancel in c# .net

我的系統存在超時過期問題。 有關更多詳細信息,請參見錯誤消息:

Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This may have occurred because all pooled connections were in use and max pool size was reached. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This may have occurred because all pooled connections were in use and max pool size was reached.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 

[InvalidOperationException: Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool.  This may have occurred because all pooled connections were in use and max pool size was reached.]
   System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +5079753
   System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +126
   System.Data.SqlClient.SqlConnection.Open() +125
   SubSonic.SqlDataProvider.CreateConnection(String newConnectionString) +37
   SubSonic.SqlDataProvider.CreateConnection() +25
   SubSonic.AutomaticConnectionScope..ctor(DataProvider provider) +62
   SubSonic.SqlDataProvider.GetReader(QueryCommand qry) +54
   SubSonic.DataService.GetReader(QueryCommand cmd) +25
   SubSonic.StoredProcedure.ExecuteTypedList() +47
   Db.Employee.FindByUsername(String sUsername) in d:\Hudson\jobs\SMART Staff - Dev\workspace\Staff\site\Classes\DataAccess\Employee.cs:426
   CurrentUser.get_SmartID() in d:\Hudson\jobs\SMART Staff - Dev\workspace\Staff\site\Classes\CurrentUser.cs:38
   DbRoleProvider.GetRolesForUser(String usernameIgnored) in d:\Hudson\jobs\SMART Staff - Dev\workspace\Staff\site\Classes\DbRoleProvider.cs:21
   System.Web.Security.RolePrincipal.IsInRole(String role) +182
   System.Web.Configuration.AuthorizationRule.IsTheUserInAnyRole(StringCollection roles, IPrincipal principal) +132
   System.Web.Configuration.AuthorizationRule.IsUserAllowed(IPrincipal user, String verb) +264
   System.Web.Configuration.AuthorizationRuleCollection.IsUserAllowed(IPrincipal user, String verb) +201
   System.Web.Security.UrlAuthorizationModule.OnEnter(Object source, EventArgs eventArgs) +9018637
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

只是徘徊,當我單擊取消或刷新C#.net中的頁面時,沒有人知道如何關閉所有已連接的數據庫連接嗎?

您應注意在代碼結束之前釋放所有對象,對於數據庫連接,應確保打開它們的時間盡可能短,例如,可以使用(using)關鍵字:

using (SqlConnection conn = new SqlConnection(connectionString))
{
  conn.Open();
  ///Use the connection ....
  ///.
  ///.
  ///.
  /// The connection will be closed and disposed automatically before the end of (using) block;
}

也許這可以幫助您:

我怎樣才能殺死用戶與sql server數據庫的所有連接?

但是不確定,您沒有告訴您正在使用什么DBMS。

打開連接后,嘗試像這樣關閉它:

 using (SqlConnection conn = new SqlConnection(connectionString))
    {  
    try          
    {    
    //
    //
    //             
    conn.Close();
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    finally
    {
    if (conn != null) conn.Close();
    }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM