如何在 C# 中保持单个 SQL Server 连接实例为多个请求打开?

编程入门 行业动态 更新时间:2024-10-26 18:20:35
本文介绍了如何在 C# 中保持单个 SQL Server 连接实例为多个请求打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have a Web API which contains database insert logic (ado) 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 的另一个实例打开,并且连接池中有可用的连接时,将使用该连接.事实上,SQL 上的 Microsoft 文档页面服务器连接池明确指出:

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 连接实例为多个请求打开?

本文发布于:2023-11-06 17:46:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564369.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   实例   如何在   Server   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!