How to connect a new query script with SSMS add-in?

Posted by squillman on Stack Overflow See other posts from Stack Overflow or by squillman
Published on 2009-06-19T04:28:55Z Indexed on 2010/05/03 15:18 UTC
Read the original article Hit count: 425

I'm trying to create a SSMS add-in. One of the things I want to do is to create a new query window and programatically connect it to a server instance (in the context of a SQL Login). I can create the new query script window just fine but I can't find how to connect it without first manually connecting to something else (like the Object Explorer).

So in other words, if I connect Obect Explorer to a SQL instance manually and then execute the method of my add-in that creates the query window I can connect it using this code:

ServiceCache.ScriptFactory.CreateNewBlankScript(
     Editors.ScriptType.Sql,
     ServiceCache.ScriptFactory.CurrentlyActiveWndConnectionInfo.UIConnectionInfo,
     null);

But I don't want to rely on CurrentlyActiveWndConnectionInfo.UIConnectionInfo for the connection. I want to set a SQL Login username and password programatically.

Does anyone have any ideas?

EDIT:

I've managed to get the query window connected by setting the last parameter to an instance of System.Data.SqlClient.SqlConnection. However, the connection uses the context of the last login that was connected instead of what I'm trying to set programatically. That is, the user it connects as is the one selected in the Connection Dialog that you get when you click the New Query button and don't have an Object Explorer connected.

EDIT2:

I'm writing (or hoping to write) an add-in to automatically send a SQL statement and the execution results to our case-tracking system when run against our production servers. One thought I had was to remove write permissions and assign logins through this add-in which will also force the user to enter a case # canceling the statement if it's not there. Another thought I've just had is to inspect the server name in ServiceCache.ScriptFactory.CurrentlyActiveWndConnectionInfo.UIConnectionInfo and compare it to our list of production servers. If it matches and there's no case # then cancel the query.

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about sql-server-management-stu