ASP.NET使用C#代码终止所有打开的SQL Server连接

Ari*_*MAZ 0 c# sql-server asp.net connection-pooling

有没有办法杀死ASP.NET代码中的所有打开的SQL Server连接?我为什么要这样做?因为一些团队开发人员忘记关闭他们的联系,我厌倦了提醒他们.

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand SqlCommand = new SqlCommand();
connection.Open();
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 8

杀死所有打开的连接会非常糟糕:你不知道这些连接在asp.net这样的多线程代码中做了什么.直接回答:除非你手动跟踪所有连接,否则这是不可能的 - 但是跟踪它们的任何时间都可以更好地用于修复底层问题.

这里最合适的选择是添加using关于连接使用的语句.