如何保持 SqlConnection 始终打开

发布于 2024-08-25 05:43:44 字数 623 浏览 3 评论 0原文

如何在 .Net 应用程序执行期间始终保持 SqlConnection(或使用其他组件)打开(连接)?

我需要这个,因为我的应用程序需要使用此命令检测

 exec sp_who2 

我的应用程序有多少实例连接到 mydatabase,以限制访问(许可证控制)。

示例

A) 我的应用程序从 location1 执行,

  1. exec sp_who2 检查连接到 sql 服务器的应用程序数量
  2. 如果我的应用程序数量 MaxLicencesConnected 然后启动我的应用程序并打开 sqlconnection

B) 我的应用程序从 location2 执行,

  1. 使用 exec sp_who2 检查连接到 sql 服务器的应用程序数量,
  2. 如果我的应用程序的数量 >= 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

  1. check the number of my apps connected to the sql server using exec sp_who2
  2. if the number of my applications < MaxLicencesConnected then start my app and open a sqlconnection

B) my app executed from location2

  1. check the number of my apps connected to the sql server using exec sp_who2
  2. if the number of my applications >= MaxLicencesConnected then close my application

sorry for my english.

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

攒眉千度 2024-09-01 05:43:44

简单地有一个表,在程序启动时添加一条记录并在程序退出时删除它不是更容易吗?始终保持与 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).

夕色琉璃 2024-09-01 05:43:44

连接池使连接保持活动状态:

http://msdn.microsoft.com/en -us/library/8xx3tyca.aspx

Connection pooling keeps the connection alive:

http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx

画中仙 2024-09-01 05:43:44

使用连接池。在连接字符串中设置最大池大小最小池大小以及池化

Use connection pooling. Set Max Pool Size and Min Pool Size and Pooling in your connection string.

墨小墨 2024-09-01 05:43:44

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文