C#-我应该使用静态数据库连接 [英] C# - Should I use static database connection

查看:61
本文介绍了C#-我应该使用静态数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用于连接到Orace数据库的应用程序中,我总是创建一个新连接,将其打开,执行OracleCommands,最后再将其关闭.最近,我认为实现静态连接将是一个更好的主意.说我有一个静态连接,可以从任何地方访问.每次需要连接数据库时,我都可以检查静态连接的状态,如果尚未打开,请打开它,然后再关闭它.您认为这将是有益的还是存在更多的不利条件?

In my application to connect to Orace database I always create a new connection, open it, execute OracleCommands and finally close it afterwards. Recently I thought implementing a static connection would be a better idea. Say I have a static connection that I can access from anywhere. Each time I need to connect to database I can check for the state of my static connection,open it if it's not already open and close it afterwards. Do you think this would be beneficial or there are more disadvantages?

推荐答案

我假设您在这里使用ODBC,因为您没有确切说明并且通常使用它...

I'm assuming you're using ODBC here because you haven't stated exactly and it's normally used...

否,您应该每次都使用一个新的连接,这是Microsoft建议的标准做法.如果您使用的是ODBC等,则Windows将管理这些连接,将它们缓存以供重用,这样可以更轻松地管理事物的生命周期.

No, you should use a new connection each time, this is the standard practise that Microsoft recommend. If you're using ODBC etc then windows manages these connections, caching them for re-use and it makes it easier to manage the lifetime of things.

如果使用静态连接,则可能过早处置它,或者在不知情的情况下将其关闭.总的来说,这有点尴尬和过早的优化.

If you use a static connection, you might dispose of it to early or have closed it without knowing. In general it's just a bit more awkward and a premature optimization.

要部署高性能应用程序,您必须经常使用 连接池.但是,当您使用.NET Framework数据时 ODBC的提供程序,您不必启用连接池 因为提供商会自动进行管理.

To deploy high-performance applications, you frequently must use connection pooling. However, when you use the .NET Framework Data Provider for ODBC, you do not have to enable connection pooling because the provider manages this automatically.

有关更多信息,请参见 OdbcConnection .

See OdbcConnection for more info.

这篇关于C#-我应该使用静态数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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