如何保持 SqlConnection 始终打开
如何在 .Net 应用程序执行期间始终保持 SqlConnection(或使用其他组件)打开(连接)?
我需要这个,因为我的应用程序需要使用此命令检测
exec sp_who2
我的应用程序有多少实例连接到 mydatabase,以限制访问(许可证控制)。
示例
A) 我的应用程序从 location1 执行,
exec sp_who2
检查连接到 sql 服务器的应用程序数量- 如果我的应用程序数量 MaxLicencesConnected 然后启动我的应用程序并打开 sqlconnection
B) 我的应用程序从 location2 执行,
- 使用
exec sp_who2
检查连接到 sql 服务器的应用程序数量, - 如果我的应用程序的数量 >= MaxLicencesConnected 然后关闭我的应用程序
抱歉我的英语。
提前致谢。
How can maintain a SqlConnection (or using another component) open (connected) always during the execution of my .Net app?
I need this because my app needs to detect using this commnad
exec sp_who2
how many instances of my app are connected to mydatabase, to restrict the access (license control).
example
A) my app executed from location1
- check the number of my apps connected to the sql server using
exec sp_who2
- if the number of my applications < MaxLicencesConnected then start my app and open a sqlconnection
B) my app executed from location2
- check the number of my apps connected to the sql server using
exec sp_who2
- if the number of my applications >= MaxLicencesConnected then close my application
sorry for my english.
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单地有一个表,在程序启动时添加一条记录并在程序退出时删除它不是更容易吗?始终保持与 SQL Server 的连接打开是相当昂贵的。
从技术上讲,有人可以删除记录并启动一个新实例,但显然如果他们这样做,那么第一个实例将停止工作(假设它时不时地检查自己的记录是否存在)。
Wouldn't it be easier to simply have a table where you add a record when the program starts and remove it when the program exits? Keeping a connection to SQL Server open all the time is quite expensive.
Technically, someone could come along a delete the record and start up a new instance, but obviously if they do that then the first instance is going to stop working (assuming it checks for the presense of it's own record every now and then).
连接池使连接保持活动状态:
http://msdn.microsoft.com/en -us/library/8xx3tyca.aspx
Connection pooling keeps the connection alive:
http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx
使用连接池。在连接字符串中设置最大池大小和最小池大小以及池化。
Use connection pooling. Set Max Pool Size and Min Pool Size and Pooling in your connection string.
Myabe 您可以设置一个连接对象,打开它并将其存储在全局(静态)变量中。我相信,您还必须设置连接的超时时间,否则如果闲置,它会自动关闭。
坦率地说,我不认为保持连接开放是一个好主意,因为它们很昂贵。
Myabe you can set a connection object, open it and store in a global (static) variable. I beleive, you would also have to set the timeout of the connection, otherwise it will be closed automaticaly if let idle.
Franly speaking, I dont think keeping a connection open is a good idea, as they are expensive.