Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can maintain a SqlConnection open always

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.

like image 979
Salvador Avatar asked Mar 16 '10 03:03

Salvador


2 Answers

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).

like image 88
Dean Harding Avatar answered Oct 05 '22 13:10

Dean Harding


Connection pooling keeps the connection alive:

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

like image 25
Max Toro Avatar answered Oct 05 '22 13:10

Max Toro