如何在C#中保持单个SQL Server连接实例对多个请求打开? [英] How to keep single SQL Server connection instance open for multiple request in C#?

查看:101
本文介绍了如何在C#中保持单个SQL Server连接实例对多个请求打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web API,其中包含C#中的数据库插入逻辑(ado.net)。当多个用户(例如100个用户)调用Web API时,每次针对多个请求打开和关闭SQL Server连接时。这会降低性能。

I have a Web API which contains database insert logic (ado.net) in C#. When multiple users (e.g. 100 users) call the Web API, every time a SQL Server connection is opened and closed for multiple requests. It slows down performance.

如何为多个请求保持单个SQL Server连接的活动?我必须保持SQL连接仅打开一次并在一段时间后关闭,以便在此期间它应该考虑多个请求并在数据库中插入记录。

How can I keep a single SQL Server connection live for multiple requests? I have to keep SQL connection open only once and close after some time so that during that time it should consider multiple request and insert records in database.

请建议。

推荐答案

ADO.NET的 SqlConnection 正在实现连接池。
这意味着当您关闭或处置 SqlConnection 的实例时,基础连接只是返回到池中。当打开 SqlConnection 的另一个实例,并且连接池中有可用连接时,将使用该连接。

实际上,Microsoft docs页面在 SQL Server连接池明确指出:

ADO.NET's SqlConnection is implementing a connection pool. This means that when you close or dispose an instance of SqlConnection, the underlying connection simply returns to the pool. When another instance of SqlConnection is opened, and a connection is available in the connection pool, that connection will be used.
In fact, Microsoft docs page on SQL Server Connection Pooling clearly states:


警告

我们强烈建议您始终关闭连接使用完后,连接将返回到池中。您可以使用Connection对象的Close或Dispose方法,或通过在C#中的using语句或Visual Basic中的using语句中打开所有连接来执行此操作。未显式关闭的连接可能不会添加或返回到池中。有关更多信息,请参见使用语句或如何:处置Visual Basic的系统资源。

Caution
We strongly recommend that you always close the connection when you are finished using it so that the connection will be returned to the pool. You can do this using either the Close or Dispose methods of the Connection object, or by opening all connections inside a using statement in C#, or a Using statement in Visual Basic. Connections that are not explicitly closed might not be added or returned to the pool. For more information, see using Statement or How to: Dispose of a System Resource for Visual Basic.

这意味着最佳实践 SqlConnection 的用法是这样的:

This means that the best practice way of using SqlConnection is this:

using(var con = new SqlConnection(connectionString))
{
    // your sql stuff goes here...
}

BTW, SqlCommand SqlDataReader SqlDataAdapter 还实现了 IDisposable 接口,因此也需要在 using 语句的上下文中使用它们:

BTW, SqlCommand, SqlDataReader and SqlDataAdapter also implements the IDisposable interface, so they too needs to be used in the context of the using statement:

using(var con = new SqlConnection(connectionString))
{
    using(var cmd = new SqlCommand(sql, con))
    {
        // prepare command here - parameters and stuff like that

        // either
        using(var reader = cmd.ExecuteReader())
        {

        }

        // or 
        using(var adapter = new SqlDataAdapter(cmd))
        {

        }

    }
}

这篇关于如何在C#中保持单个SQL Server连接实例对多个请求打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆