views:

428

answers:

2

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.

+1  A: 

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.

Mladen Prajdic
Ooh, that's a drag... You're actually one of the people I was hoping could help me after finding your add-in site last night! I've edited the question to include a summary of the project.
squillman
To your edit: not quite. I still want the query to go through on a production system, I just want to ensure that the query and results run against our production servers get sent to our issue tracker. I'm not worried about the add-in being disabled, it's just going to be for our dept. That solution would be an alternate IF I can't programatically log someone in...
squillman
i'm sorry but i don't really see a viable way for you to do this.
Mladen Prajdic
Yeah, I'm writing it off for now. Like I said above, God willing MS will add extensibility to SSMS soon! Thanks for the time!!
squillman
If they added it very soon, when do you think you would see it? Probably not for another two or three years.
John Saunders
i'm guessing they'll add extensibility for the version after 2008 R2. but that's just my guess. i have no info that'd support that claim.
Mladen Prajdic
A: 

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.