4

enter image description here When you edit a sql file in the Transact-SQL Editor in VS, you can push the connect button to bring up a db connection dialogue. How can I automate this from a VS extension? I can catch the fileOpen event no problem. Does anyone know where in DTE the connect command is hiding? Not just popping the dialogue, but a method that takes a connection string and connects the editor window.

Update

I've listed all my VS commands. The one corresponding to the connect button is "SQL.TSqlEditorConnect". I don't seem able to call it without getting in an endless loop, and I suspect it's just the command for opening the dialog, not creating the connection.

bbsimonbb
  • 27,056
  • 15
  • 80
  • 110

2 Answers2

1

use the 'sqlcmd' as follows:

sqlcmd -S <ComputerName>\<InstanceName> -i C:\Temp\ClearTables.sql

As seen : Executing set of SQL queries using batch file?

edit:: Have you tried:

OPEN DATABASE [FileName | ?] [EXCLUSIVE | SHARED] [NOUPDATE] [VALIDATE]

As seen here -> https://msdn.microsoft.com/en-us/library/w01w7w4c%28v=vs.80%29.aspx

**Second Edit:: ** Have you tried SSH?

Community
  • 1
  • 1
kpie
  • 9,588
  • 5
  • 28
  • 50
  • Sorry maybe I wasn't clear. I don't want to automate running the sql. I want to automate connecting the editor window. From within my VS extension, I want to call some envdte method, supplying a connection string, which will put the editor window in the connected state. – bbsimonbb May 26 '16 at 08:34
  • The second suggestion appears to be a visual foxpro command? I don't think we're getting closer. – bbsimonbb May 26 '16 at 13:09
0

Thanks to Kevin Cunnane of MS's SSDT team, the answer is a service called the ISqlServerObjectExplorerService. This has an OpenQuery method that takes in a connection string.

This is available via the standard VS extensibility model (in your package, call either GetService or GetGlobalService, referencing this service).

ISqlServerObjectExplorerService service = DataPackage.Instance.GetService<ISqlServerObjectExplorerService>(typeof(ISqlServerObjectExplorerService));
if (service != null)
{
    service.OpenQuery(new SqlConnectionStringBuilder(myConnectionString));
}
bbsimonbb
  • 27,056
  • 15
  • 80
  • 110