Question

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.

Was it helpful?

Solution

I have not found a way to do this, since there appears to be no way to hook into the connection dialog window.

what are you working on?

EDIT: If i understand correctly you want to intercept the query being run and if it matches the production server cancel it else send the text and results to a db? hmm... while this would be possible but is a real major pain in the behind, and i wouldn't use an add-in for this. plus an add-in can be disabled, uninstalled etc. you better try doing this with proper security setup on your production server.

OTHER TIPS

Just in case anybody else is searching for a way to create and connect a query window programmaticaly - a short example for SSMS 2012:

UIConnectionInfo u =new UIConnectionInfo();

u.ServerName = "TestPC\\ServerInstance";

u.ServerType = new Guid("8c91a03d-f9b4-46c0-a305-b5dcc79ff907");

u.AuthenticationType = 0;//Use AuthenticationType = 1 for SQL Server Authentication

u.AdvancedOptions.Set("DATABASE", "AdventureWorks2012");

IScriptFactory scriptFactory = ServiceCache.ScriptFactory;

if(scriptFactory != null)
{
  scriptFactory.CreateNewBlankScript(ScriptType.Sql, u, null);
} 

This code snippet will open a new query window connected to specified server and database with windows auth.

I haven't tried this yet but didn't know if you have given it a shot.

Microsoft.SqlServer.Management.Smo.RegSvrEnum.UIConnectionInfo u = 
    new Microsoft.SqlServer.Management.Smo.RegSvrEnum.UIConnectionInfo();

Should allow you to make your own connection. But like I said, I haven't tested it yet. You'll need a reference to Microsoft.SqlServer.RegSvrEnum.dll.

Let me know if it works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top