Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open new database connection and query window in SSMS through C# application?

Tags:

c#

ssms

I have a C# application that, when a user clicks a button, will open SQL Server Management Studio query editor with a specified server and database connection. What I would like to do is be able to have this same functionality, but with an already running instance of SSMS (not start a new process).

My code so far:

if (IsProcessOpen("Ssms") == false)
        {
            Process ssms = new Process();
            ssms.StartInfo.FileName = "C:\\Program Files (x86)\\Microsoft SQL Server\\110\\Tools\\Binn\\ManagementStudio\\Ssms.exe";
            ssms.StartInfo.Arguments = "-S " + StaticVariables.Getserver + " -d " + StaticVariables.Getdatabase;
            ssms.Start();
        }
        else
        {
            //In already running SSMS process, open connection to server and database with new query window.
        }
like image 519
Fienix Avatar asked Oct 24 '12 17:10

Fienix


1 Answers

As far my research goes, there is no API available to interact with an existing SSMS processs. (http://stackoverflow.com/questions/2334435/how-do-i-programmatically-open-a-new-tab-in-an-open-instance-of-sql-management-s)

You could try doing a hack involving SendKeys, but this would have a downside of not knowing the current state of the SSMS window (if it has any active windows open or anything)

If you could share the original requirement, we may look at some better alternatives.

like image 96
tempidope Avatar answered Sep 17 '22 13:09

tempidope