In C#, How can I check the process bind to the same database in mssql?

Hi there;

In C#, how can I check the process bind to the same database in mssql?

There are several instances of same application and I need to learn which process/instance reaches for the db in mssql.

Kind regards.
LVL 12
jazzIIIloveAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dimajCommented:
The easiest way is to have some sort of a text file that would get updated every time a program connects or disconnects to a db.
Your text file could contain a list of PIDs that are currently connected.
0
jazzIIIloveAuthor Commented:
So, this is where multithreading comes in charge?

note that, this check will be done in program itself, so by default it's connecting to the database. I want whether other same instances connect or not.
0
dimajCommented:
Sure,  you could have a thread running in the background that tracks timestamp of last file modification and if that time has changed, fire off an event strung
0
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

dimajCommented:
Sorry, my hand slipped on the phone...
Your event well say that another connection or disconnection took place.
0
jazzIIIloveAuthor Commented:
cool, but I need a brief example, could you provide me one?

Kind regards.
0
dimajCommented:
Here's a brief code of what is needed to create an event:
 
// create an event
public delegate void FileUpdatedEventHandler (object sender, EventArgs e);

public event FileUpdateEventHandler Updated;

private _lastWrite = 0;
private void CheckFileChanged () {
  string myFile = @"C:\myconnections.txt";
  FileInfo fi = new FileInfo(myFile);
  if (_lastWrite == 0) {
    _lastWrite = fi.LastWriteTimeUtc;
  }
  else if (_lastWrite != fi.LastWriteUtc) {
    if (Updated != null)
      // fire off the event
      Updated(this, EventArgs.Empty); // change this if you want to pass some data
  }
}

Open in new window


Here's a good example on how to work with events: http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.