在 C# 应用程序中,数据库连接应该创建一次,还是每次执行 SQL 语句时创建?
在 C# 应用程序中,OleDBConnection 应该创建一次,还是每次执行 SQL 语句时创建?
我正在看别人写的C#代码。每个 SQL 语句之前都会创建一个 OleDbConnection
对象,该对象的连接字符串指向 MDB 数据库。
每次都创建一个 OleDbConnection 对象是否合适,或者应该在应用程序启动时创建该对象并从那时起使用该对象。
In a C# application, should a OleDBConnection
be created once, or every time a SQL statement is executed?
I am looking at C# code written by someone else. Every SQL statement is preceded with the creation of an OleDbConnection
object whose connection string points to an MDB database.
Is it appropriate to create an OleDbConnection object every time, or should the object be created at the start of the application and used from then on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据使用 ADO.NET 的最佳实践:
According to Best Practices for Using ADO.NET:
假设您的数据库可以使用连接池,那么您可能希望为每次调用数据库打开和关闭连接。这允许您仅在需要时才使用数据库连接的有限资源,然后在完成调用后立即将其返回到池中供其他调用者使用。如果您保留该连接,您很快就会耗尽有限的资源,即数据库的可用连接总数,从而严重阻碍应用程序的可扩展性和性能。
我通常使用 using 语句来确保使用后连接关闭 - 见下文:
享受!
Assuming connection pooling is available for your database, which likely it is you will want to open and close a connection for every call to your database. This allows you to only use the finite resource of a database connection only when you need it and then return it to the pool for other callers to use as soon as you finished making the call. If you hold on to the connection you will soon run out of the finite resource, that being the total available connections to the database, and hence severely hinder the scalability and performance of your application.
I usually use a using statement to ensure the connection is close after use - see below:
Enjoy!
这取决于具体情况。
如果您要连续执行多个语句,那么从性能角度来看,您最好打开一次,执行所有语句,然后关闭它,
如果您询问在程序运行时打开连接启动并保持打开状态直到程序关闭,无论发生什么情况,然后就不会。完成后立即关闭它。最好不要让连接保持打开状态。
人们似乎从未想到的另一个因素是后来的维护程序员,他们必须跟踪代码并跟踪连接在哪里打开以及何时关闭。例如,假设您有一个程序访问数据库,然后分支为几个其他函数,每个函数都可能需要连接。跟踪代码中的那些东西是一场噩梦。
话又说回来,这个因素对于正确的操作和性能来说是次要的,但在复杂的应用程序中仍然需要考虑。
主要因素是它将如何影响性能与保持连接打开的情况。您需要根据每种情况做出决定。
It depends on the situation.
If you're going to be executing several statements in a row, then you're better off performance-wise to open it once, execute all the statements, and then close it,
If you're asking about opening the connection when the program starts and keeping it open until the program closes regardless of what's going on, then no. Close it as soon as you're done with it. It's better not to leave a connection hanging open.
Another factor that people never seem to think of is the maintenance programmer coning along later, who has to trace the code and keep track of where the connection is opened and when it is closes. Say, for example, that you have a program that accesses a database, then branches off into several other functions, each of which may need the connection. Tracking that stuff in code is a nightmare.
Then again, this factor is secondary to proper operation and performance, but still something to consider in a complex app.
The main factor is how it will affect performance vs. the cose of keeping a connection open. You need to decide that in each situation.
仅当您直接在数据库上执行活动时,才应打开与数据库的连接。在应用程序执行其他活动时保持与数据库的打开连接,一旦连接池达到其限制,可能会阻止其他用户访问数据库。
如果您要在客户端 PC 上执行 CPU 密集型功能,则应在关闭连接后执行此任务,以免占用连接。
但是,如果您要执行一系列数据库功能,则可以通过在单个打开的连接中同时执行这些操作来提高客户端的性能。
Your connection to the database should only be open while you are performing activity directly on the database. Maintaining an open connection to the database while your application is performing other activity can block other users from accessing the database once your connection pool reaches its limit.
If you have a CPU-intensive function to perform on the client PC, then you should perform this task once you have closed your connection so that you do not tie up the connection.
But if you have a series of database functions to perform, your client's performance can be improved by doing these together in a single open connection.
每次需要数据库中的某些内容时,您都应该打开一个连接,然后关闭它。由连接池来决定何时物理关闭连接。
You should open a connection every time you need something from the db and close it afterwards. Leave to the connection pool to decide when physically close the connection.