• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

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.
0
jazzIIIlove
Asked:
jazzIIIlove
  • 4
  • 2
1 Solution
 
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now