ASP.NET / ADO.NET:在.NET对象内处理许多数据库连接? [英] ASP.NET/ADO.NET: Handling many database connections inside a .NET Object?

查看:150
本文介绍了ASP.NET / ADO.NET:在.NET对象内处理许多数据库连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个.NET对象,它使用数据库进行大量的读/写操作。在这个对象(或使用它的asp页面)的整个生命周期中,它可以通过1到10次的查询/更新命中数据库。

We have a .NET object that does a lot of reading/writing with the database. Throughout the life cycle of this object (or the asp page that uses it), it may hit the database with a query/update anywhere from 1 to 10 times.

每次对象需要命中数据库时,它不是打开和关闭数据库连接,而是在实例化期间只打开与数据库的连接,然后关闭连接对象的终止事件。这是一个坏的做法吗?

Instead of opening and closing a database connection every time the object needs to hit the database, it simply opens a connection to the database during instantiation and then closes the connection during the object's termination event. Is that a bad practice?

假设是因为对象在每次实例化(通常多次)时都会访问数据库,所以最好打开一个连接在对象的生命的开始,然后关闭它在结束。

The assumption was that since the object hits the database every time it's instantiated (and usually multiple times), it would be better to just open a connection at the beginning of the object's life and then close it at the end.

另一种方法是在每个查询/操作之前和之后打开和关闭数据库连接。

The other alternative is to open and close the database connection before and after each query/operation.

为了最大限度地提高性能,这里的最佳做法是什么?

What's the best practice here in order to maximize performance?

**** update ****
感谢提示。任何人都可以更多地谈论在对象的实例化/终止事件中打开/关闭连接的方法,以及这样做的后果?

****update**** Thanks for the tips guys. Can anyone speak more to the method of opening/closing a connection inside an object's instantiation/termination events and the repercussions of doing so?

推荐答案

根据需要打开关闭连接。 ADO.NET内置连接池工作。

Open an close the connection as needed. ADO.NET has built in connection pooling that works. You will not notice any performance issues unless you are doing this in a loop with thousands of open/closes.

编辑
请参阅我应该在我的数据访问层中持久化sqlconnection吗? / a>有关连接持久性的陷阱的更多信息。

edit See Should I persist a sqlconnection in my data access layer? for more information as to the pitfalls of connection persistence.

这篇关于ASP.NET / ADO.NET:在.NET对象内处理许多数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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