如何处理在数据访问层连接(.NET)如何处理、数据、NET

2023-09-04 02:32:05 作者:一秒心动

喜 我写一个数据访问层。我感到困惑的系统管理连接。 我知道,.NET使用连接池。但我不希望打开,并在所有DML操作或所有SQL查询的关闭数据库连接。我该如何处理呢?何时何地(也许在它使用的数据访问层和数据访问层全球ASAX)的连接应加以管理?

Hi I am writing a data access layer. I am confused about managing connections in the system. I know that .net uses connection pooling. But I do not want to open and close database connections in all dml operations or in all sql query's. How can I handle this? Where and when ( maybe in global asax which uses data access layer or in the data access layer ) the connections should be managed?

推荐答案

除非你正在运行一个批处理语句,则应该打开和关闭的SQL连接,为每个查询。

You should open and close sql connections for each query, unless you are running a batch of statements.

打开晚了,提前关门是如何你应该总是处理数据库的连接。

"Open late, close early" is how you should always handle database connections.

如果你要它在传统的方式(使自己的查询),MS已经写了一个漂亮的数据访问接口。该企业库(应用程序块)的数据都漂亮合式花俏。

If you are going about it in the traditional manner (making your own queries), MS has already written a nice data access interface. The enterprise library (application blocks) for data have all of the nice well formed bells and whistles.

如果你不想费心编写查询,我建议你看的 LINQ2SQL 或 linq2EF (preferred)。他们将大大简化您的编码。

If you dont want to bother with writing queries, I suggest you look at linq2Sql or linq2EF (preferred). They will greatly simplify your coding.

 
精彩推荐
图片推荐