Singleton SqlConnection对象 [英] Singleton SqlConnection object

查看:89
本文介绍了Singleton SqlConnection对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我已经为一个类实现了单例模式来保存一个

的SqlConnection对象

将是使用thruout应用程序来创建命令。我的应用程序

是一个简单的Windows窗体

连接到MSDE数据库的应用程序。我昨天在这个相同的主题上阅读了这个主题

,但它只提到了ASP.NET应用程序。任何人都可以告诉我,在应用程序中有一个单独的SqlConnection对象是否是个好主意

。请注意

,Singleton类

已配置为线程安全的。下面是我用来调用实例

我的Singleton类的一些代码:


SqlCommand command = DB_Connection .Instance.Connection.CreateCommand();


完成连接后,我将其关闭:


command.Connection。打开();


//在这里做点什么


command.Connection.Close();


我也处理命令对象以确保我释放资源。

任何信息,建议,评论

将受到欢迎!

Hello,

I have implemented the singleton pattern for a class to hold a
SqlConnection object that
will be used thruout the application to create commands. My application
is a simple Windows Form
Application that connects to a MSDE Database. I was reading the thread
yesterday on this very
same subject, but it only mentioned ASP.NET Applications. Can anyone
tell me if it''s a good idea
to have one single SqlConnection Object in the application. Please note
that the Singleton Class
has been configured to be thread-safe. Below is some of the code I am
using to call the instance
of my Singleton class:

SqlCommand command = DB_Connection.Instance.Connection.CreateCommand();

Once I am done with the connection, I close it down:

command.Connection.Open();

//Do stuff here

command.Connection.Close();

I also dispose of the command object to make sure I free up resources.
Any information, suggestions, critiques
will be welcomed!

推荐答案



" ed_p" < ed*@noe-mail.com>在消息中写道

新闻:uV ************** @ TK2MSFTNGP09.phx.gbl ...

"ed_p" <ed*@noe-mail.com> wrote in message
news:uV**************@TK2MSFTNGP09.phx.gbl...
你好,
我已经为一个类实现了单例模式来保存一个
SqlConnection对象,该对象将在应用程序中用于创建命令。我的应用程序
是一个连接到MSDE数据库的简单Windows窗体
应用程序。我昨天在这个相同的主题上阅读了这个主题,但它只提到了ASP.NET应用程序。任何人都可以告诉我,如果在应用程序中有一个单独的SqlConnection对象是一个好主意。请注意
单例类
已配置为线程安全。下面是我用来调用我的Singleton类的实例
的一些代码:
Hello,

I have implemented the singleton pattern for a class to hold a
SqlConnection object that
will be used thruout the application to create commands. My application
is a simple Windows Form
Application that connects to a MSDE Database. I was reading the thread
yesterday on this very
same subject, but it only mentioned ASP.NET Applications. Can anyone tell
me if it''s a good idea
to have one single SqlConnection Object in the application. Please note
that the Singleton Class
has been configured to be thread-safe. Below is some of the code I am
using to call the instance
of my Singleton class:




在winforms应用程序中,所有代码都运行在同一个线程上(unliess you

明确使用线程)。所以使用单例连接对象就好了。\\ b
就好了。如果您需要从后台线程访问数据库,那么

将不希望使用您的全局连接。


David



In a winforms application all the code runs on the same thread (unliess you
explicitly use threading). So using a singleton connection object will be
just fine. If you need to access the database from a background thread, you
would not want to use your "global" connection.

David


戴夫,


感谢您的快速回复,所以即使我让类线程安全

我仍​​然无法在背景线程中使用它?


David Browne写道:
Dave,

Thanks for the quick reply, so even though I made the class thread-safe
I still would not be able to use it from a backgroud thread?

David Browne wrote:
" ed_p" < ed*@noe-mail.com>在消息中写道
新闻:uV ************** @ TK2MSFTNGP09.phx.gbl ...
"ed_p" <ed*@noe-mail.com> wrote in message
news:uV**************@TK2MSFTNGP09.phx.gbl...
你好,
我已经为一个类实现了单例模式来保存一个
SqlConnection对象,该对象将在应用程序中用于创建命令。我的应用程序
是一个连接到MSDE数据库的简单Windows窗体
应用程序。我昨天在这个相同的主题上阅读了这个主题,但它只提到了ASP.NET应用程序。任何人都可以告诉我,如果在应用程序中有一个单独的SqlConnection对象是一个好主意。请注意
单例类
已配置为线程安全。下面是我用来调用我的Singleton类的实例
的一些代码:
Hello,

I have implemented the singleton pattern for a class to hold a
SqlConnection object that
will be used thruout the application to create commands. My application
is a simple Windows Form
Application that connects to a MSDE Database. I was reading the thread
yesterday on this very
same subject, but it only mentioned ASP.NET Applications. Can anyone tell
me if it''s a good idea
to have one single SqlConnection Object in the application. Please note
that the Singleton Class
has been configured to be thread-safe. Below is some of the code I am
using to call the instance
of my Singleton class:



在winforms应用程序中,所有代码都在同一个线程上运行(unliess you
明确使用线程)。所以使用单例连接对象就好了。如果您需要从后台线程访问数据库,那么您将不希望使用您的全局数据库。连接。

大卫


In a winforms application all the code runs on the same thread (unliess you
explicitly use threading). So using a singleton connection object will be
just fine. If you need to access the database from a background thread, you
would not want to use your "global" connection.

David





" ed_p" < ed*@noe-mail.com>在消息中写道

新闻:uV ************** @ TK2MSFTNGP09.phx.gbl ...

"ed_p" <ed*@noe-mail.com> wrote in message
news:uV**************@TK2MSFTNGP09.phx.gbl...
I已经实现了一个类的单例模式来保存一个
SqlConnection对象,该对象将在应用程序中用于创建命令。
I have implemented the singleton pattern for a class to hold a
SqlConnection object that
will be used thruout the application to create commands.




为什么?


"当您使用.NET Framework数据提供程序进行SQL Server时,您不需要启用连接池,因为提供程序管理此连接池/>
自动"


通过在as

bb上创建SqlConnection,你将承担唯一的开销。基础是对象创建本身。如果这对你的

应用程序来说是个问题,那么我认为使用单例是有道理的。否则,我会

没有。



Why?

"When you use the .NET Framework Data Provider for SQL Server, you do not
need to enable connection pooling because the provider manages this
automatically"

The only overhead you''ll incurr by creating a SqlConnection on an "as
needed" basis is the object creation itself. If that''s a problem for your
application then I suppose using a singleton makes sense. Otherwise, I would
not.


这篇关于Singleton SqlConnection对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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