Question

I am trying to create a connected query window from Visual Studio 2012 addin.

This is the code i am using:

        UIConnectionInfo u = new UIConnectionInfo
        {
            ServerName = serverName,
            ServerType = new Guid(serverType),
            AuthenticationType = authType
        };

        u.AdvancedOptions.Set("DATABASE", databaseName);
        u.AdvancedOptions.Set("PACKET_SIZE", "4096");
        u.AdvancedOptions.Set("CONNECTION_TIMEOUT", "15");
        u.AdvancedOptions.Set("EXEC_TIMEOUT", "0");
        u.AdvancedOptions.Set("ENCRYPT_CONNECTION", "False");
        u.AdvancedOptions.Set("USE_CUSTOM_CONNECTION_COLOR", "False");
        u.AdvancedOptions.Set("CUSTOM_CONNECTION_COLOR", "-986896");

        u.ApplicationName = "Microsoft SQL Server Data Tools, T-SQL Editor";
        u.UserName = userName;

        if(authType == 1)
        {
            u.Password = password;
        }

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

All properties in the UIConnectionInfo are set to the exact values as when you open a query window manualy from Visual Studio 2012.

scriptFactory.FileOpenMode is set to Connected.

I 've even tried to pass an SQLConnection object with opened and closed connection to CreateNewBlankScript ( this helped with Visual Studio 2010 ), but with no result.

Among other assemblies, I have referenced SQLEditors.dll and SQLWorkbench.Interfaces , both 11.0 versions.

Any info would be really helpful.

Thanks.

Was it helpful?

Solution

Where UIConnectionInfo and SqlConnection is your connection and queryWindow currently opened window

private static void SetConnectionForQueryWindow(Window queryWindow, 
UIConnectionInfo uiConnectionInfo, SqlConnection conn)
    {
        object tempQueryWindow;
        Dictionary<string, object> openedQueryWindows;
        List<Window> temp = new List<Window> { queryWindow };

        openedQueryWindows = GetAllOpenedQueryWindows2012(temp);
        openedQueryWindows.TryGetValue(queryWindow.Document.FullName, out tempQueryWindow);

        if(tempQueryWindow == null)
        {
            return;
        }

        Assembly sqlEditor = Assembly.LoadFrom(tempQueryWindow.GetType().Assembly.Location);
        var sqlScriptEditorControl = new SqlEditorTabbedEditorPane(sqlEditor);
        var displaySqlResultsTabControl = new DisplaySqlResultsTabControl2012(tempQueryWindow, sqlScriptEditorControl);

        try
        {
            var connectionStrategyField =
                displaySqlResultsTabControl.QueryExecutorWrapper.QueryExec.GetType()
                    .GetField("_connectionStrategy", BindingFlags.Instance | BindingFlags.NonPublic);
            var connectionStrategy = connectionStrategyField.GetValue(displaySqlResultsTabControl.QueryExecutorWrapper.QueryExec);

            var connection = connectionStrategy.GetType()
                .GetField("_connectionInfo", BindingFlags.Instance | BindingFlags.NonPublic);
            connection.SetValue(connectionStrategy, uiConnectionInfo);

            var setDbConn = connectionStrategy.GetType()
                .GetMethod("SetDbConnection", BindingFlags.NonPublic | BindingFlags.Instance);

            object[] parametersArray = { conn };
            setDbConn.Invoke(connectionStrategy, parametersArray);

        }
        catch(NullReferenceException nullReferenceException)
        {

        }
    }

internal static Dictionary<string, object> GetAllOpenedQueryWindows2012(List<Window> windows)
    {
        Dictionary<string, object> openedQuerySqlWindows = new Dictionary<string, object>();
        foreach(Window window in windows)
        {
            foreach(Window queryWindow in window.Document.ActiveWindow.Collection)
            {
                if(queryWindow.Object == null)
                {
                    continue;
                }

                if(queryWindow.Object.ToString()
                   != "Microsoft.VisualStudio.Data.Tools.SqlEditor.UI.TabbedEditor.SqlEditorTabbedEditorPane")
                {
                    continue;
                }

                object sqlEditorTabbedEditorPane = queryWindow.Object;
                if(!openedQuerySqlWindows.ContainsKey(queryWindow.Document.FullName))
                {
                    openedQuerySqlWindows.Add(queryWindow.Document.FullName, sqlEditorTabbedEditorPane);
                }
            }
        }

        return openedQuerySqlWindows;
    }

where

public class SqlEditorTabbedEditorPane
{
    private readonly FieldInfo m_displaySqlResultsControl;

    public SqlEditorTabbedEditorPane(Assembly sqlEditor)
    {
        Type sqlEditorTabbedEditorPane =
            sqlEditor.GetType(
                "Microsoft.VisualStudio.Data.Tools.SqlEditor.UI.TabbedEditor.SqlEditorTabbedEditorPane");
        m_displaySqlResultsControl = sqlEditorTabbedEditorPane.GetField(
            "_resultsControl",
            BindingFlags.NonPublic | BindingFlags.Instance);
    }

    public FieldInfo DisplaySqlResultsControl
    {
        get
        {
            return this.m_displaySqlResultsControl;
        }
    }
}

public class DisplaySqlResultsTabControl2012
{
    private readonly object m_displaySqlResultsTabControl;

    private readonly QueryExecutor m_queryExecutor;

    public DisplaySqlResultsTabControl2012(
        object scriptEditor,
        SqlEditorTabbedEditorPane sqlScriptEditorControl)
    {
        m_displaySqlResultsTabControl = sqlScriptEditorControl.DisplaySqlResultsControl.GetValue(scriptEditor);
        m_queryExecutor = new QueryExecutor(m_displaySqlResultsTabControl);            
    }

    public DisplaySqlResultsTabControl2012(object scriptEditor, SqlEditorTabbedEditorPane sqlScriptEditorControl)
    {
        m_displaySqlResultsTabControl = sqlScriptEditorControl.DisplaySqlResultsControl.GetValue(scriptEditor);
        m_queryExecutor = new QueryExecutor(m_displaySqlResultsTabControl);
    }

    public object DisplaySqlResultsTabCtr
    {
        get
        {
            return m_displaySqlResultsTabControl;
        }
    }

    public QueryExecutor QueryExecutorWrapper
    {
        get
        {
            return m_queryExecutor;
        }
    }

    public class QueryExecutor
    {
        private readonly object m_queryExecutor;

        public QueryExecutor(object displaySqlResultsTabControl)
        {
            FieldInfo queryExecutor = displaySqlResultsTabControl.GetType()
                .GetField("_queryExecutor", BindingFlags.Instance | BindingFlags.NonPublic);
            if(queryExecutor != null)
            {
                this.m_queryExecutor = queryExecutor.GetValue(displaySqlResultsTabControl);
            }
        }

        public object QueryExec
        {
            get
            {
                return m_queryExecutor;
            }
        }
    }        
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top