简体   繁体   English

在C#.net中单击取消时如何关闭所有数据库连接

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

I'm having timeout expired problem for my system. 我的系统存在超时过期问题。 See error message for more details: 有关更多详细信息,请参见错误消息:

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

Just wandering does anyone know how to close all db connection that connected when I clicked cancel or refresh the page in c# .net? 只是徘徊,当我单击取消或刷新C#.net中的页面时,没有人知道如何关闭所有已连接的数据库连接吗?

You should take care of releasing all the objects before the code ends, and for database connections you should make sure to open them as short as possible, for example you can use the (using) keyword : 您应注意在代码结束之前释放所有对象,对于数据库连接,应确保打开它们的时间尽可能短,例如,可以使用(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;
}

Maybe this can help you: 也许这可以帮助您:

how can i kill all connections of users to my sql server database ? 我怎样才能杀死用户与sql server数据库的所有连接?

But not sure, you didn't tell what DBMS you are using. 但是不确定,您没有告诉您正在使用什么DBMS。

After opening connection, try to close it like this: 打开连接后,尝试像这样关闭它:

 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.

相关问题 C# 尝试关闭未保存的Word文档时,如何知道保存提示对话框中的取消按钮是否被点击 - How to know whether cancel button is clicked in Save Prompt dialog box , when we try to close Unsaved Word document in C# 如何在c#.net中集中数据库连接? - How to central the db connection in c# .net? C#SQL何时/如何关闭连接? - C# SQL when/how do I close the connection? C#.NET远程数据库连接 - C# .NET remote DB connection 在C#中单击selectAll时如何检查所有复选框 - How to check all the checkboxes when selectAll is clicked in c# 如何在C#中关闭TCP套接字连接 - how to close tcp socket connection in c# 使用MessageBox单击“关闭”按钮[X]时如何返回值? (C#) - How to return a value when the Close button [X] is clicked using the MessageBox? (C#) C#:如何取消在表单上设置为“接受”或“取消”按钮的按钮的关闭操作? - C#: How to cancel the close action for a button that's set as the Accept or Cancel button on a form? 当应用程序关闭时,DB连接会立即关闭吗? - When application close, will DB connection close instantly? 使用insert命令通过ado.net C#访问数据库时出现“连接错误” - “ Error in connection” when use insert command to access db with ado.net C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM