如何在 VSIX 扩展中设置活动窗口 (EnvDTE.Window) 的连接?

问题描述 投票:0回答:1

我一直在绞尽脑汁试图弄清楚如何为专为 SSMS 设计的 VSIX 设置 SQLConnection。我最接近的方法是将活动窗口转换为具有 SetConnection 方法的 SqlScriptEditorControl。问题是,这似乎只是在 SSMS19 中分配 SqlScriptEditorControl,并且应用的更改实际上并没有更改 SSMS 中的连接。

Window activeWindow = activeDocument.ActiveWindow; SqlScriptEditorControl editorControl = activeWindow.Object as SqlScriptEditorControl; UIConnectionInfo newInfo = getUIConnectionfromRegisteredServer(newConnection); editorControl.SetConnection(newInfo);

有谁知道如何更改 VSIX 中的连接并将其应用于 SSMS 中的活动窗口?

我期望能够为 SSMS 中当前活动窗口/活动文档分配 UICONnectionInfo。

ssms vsix
1个回答
0
投票

请尝试这个

c#
代码并告诉我它是否有效。我无法测试,因为我没有
IDE

// Get the DTE object from the Package class
EnvDTE.DTE dte = (EnvDTE.DTE)Package.GetService(typeof(EnvDTE.DTE));

// Subscribe to the ActiveDocumentChanged event
dte.Events.DocumentEvents.ActiveDocumentChanged += OnActiveDocumentChanged;

// Method called when the active document changes
private void OnActiveDocumentChanged(Document document)
{
    if (document is EnvDTE.Window window)
    {
        try
        {
            // Try to access the SqlScriptEditorControl
            SqlScriptEditorControl editorControl = window.Object as SqlScriptEditorControl;
            if (editorControl != null)
            {
                // Get the UIConnectionInfo for your new connection
                UIConnectionInfo newInfo = getUIConnectionfromRegisteredServer(newConnection);

                // Use the appropriate method to set the connection based on SSMS version
                if (IsNewerVersionOfSSMS())
                {
                    ((EnvDTE.SqlWindow)window).CurrentServer = newInfo.ServerName;
                }
                else
                {
                    editorControl.SetConnection(newInfo);
                }
            }
        }
        catch (Exception ex)
        {
            // Handle any exceptions gracefully
            // Log or display an error message
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.