gpt4 book ai didi

c# - SSMS 可扩展性项目 - 如何研究/调试

转载 作者:太空宇宙 更新时间:2023-11-03 13:46:49 25 4
gpt4 key购买 nike

按照 this answer 的脉络关于创建 SSMS 扩展:

namespace SSMSAddin
{
using System;
using System.IO;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
using Microsoft.SqlServer.Management.UI.VSIntegration;
using System.Windows.Forms;

public class Connect : IDTExtensibility2, IDTCommandTarget
{
private DTE2 applicationObject;
private CommandEvents executeSqlEvents;
private AddIn addInInstance;

public Connect() { }

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
this.applicationObject = (DTE2)application;
this.addInInstance = (AddIn)addInInst;

this.applicationObject = (DTE2)application;
this.executeSqlEvents = this.applicationObject.Events.CommandEvents["{52692960-56BC-4989-B5D3-94C47A513E8D}", 1];
this.executeSqlEvents.BeforeExecute += this.ExecuteSqlEventsBeforeExecute;

if (connectMode == ext_ConnectMode.ext_cm_UISetup)
{
var contextGUIDS = new object[] { };
var commands = (Commands2)this.applicationObject.Commands;
string toolsMenuName = "Tools";

//Place the command on the tools menu.
//Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:
CommandBar menuBarCommandBar = ((CommandBars)this.applicationObject.CommandBars)["MenuBar"];

//Find the Tools command bar on the MenuBar command bar:
CommandBarControl toolsControl = menuBarCommandBar.Controls[toolsMenuName];
CommandBarPopup toolsPopup = (CommandBarPopup)toolsControl;

//This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
// just make sure you also update the QueryStatus/Exec method to include the new command names.
try
{
//Add a command to the Commands collection:
Command command = commands.AddNamedCommand2(this.addInInstance, "SSMSAddin", "SSMSAddin", "Executes the command for SSMSAddin", true, 59, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

//Add a control for the command to the tools menu:
if ((command != null) && (toolsPopup != null))
{
command.AddControl(toolsPopup.CommandBar, 1);
}
}
catch (ArgumentException)
{
//If we are here, then the exception is probably because a command with that name
// already exists. If so there is no need to recreate the command and we can
// safely ignore the exception.
}
}
}

private void ExecuteSqlEventsBeforeExecute(string guid, int id, object customin, object customout, ref bool canceldefault)
{
try
{
Document document = ((DTE2)ServiceCache.ExtensibilityModel).ActiveDocument;
var textDocument = (TextDocument)document.Object("TextDocument");

string queryText = textDocument.Selection.Text;

if (string.IsNullOrEmpty(queryText))
{
EditPoint startPoint = textDocument.StartPoint.CreateEditPoint();
queryText = startPoint.GetText(textDocument.EndPoint);
}

DateTime now = DateTime.Now;
// string server =
string folderPath = string.Format(@"B:\SSMS Queries\{0}", now.ToString("yyyyMMdd"));
string fileName = now.ToString("yyyyMMdd-HHmmss") + ".sql";
Directory.CreateDirectory(folderPath);
string fullPath = Path.Combine(folderPath, fileName);
File.WriteAllText(fullPath, queryText);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom) { }

public void OnAddInsUpdate(ref Array custom) { }

public void OnStartupComplete(ref Array custom) { }

public void OnBeginShutdown(ref Array custom) { }

public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
{
if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
{
if (commandName == "SSMSAddin.Connect.SSMSAddin")
{
status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
return;
}
}
}

public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if (commandName == "SSMSAddin.Connect.SSMSAddin")
{
var document = ((DTE2)ServiceCache.ExtensibilityModel).ActiveDocument;
if (document != null)
{
//replace currently selected text
var selection = (TextSelection)document.Selection;
selection.Insert(
@"Welcome to SSMS. This sample is brought to you by

SSMSBoost add-in team

Check www.ssmsboost.com for updates.",
(Int32)EnvDTE.vsInsertFlags.vsInsertFlagsContainNewText);
}

handled = true;
return;
}
}
}
}
}

代码添加了一个事件,该事件在 SSMS 2012 中的每个 SQL 执行之前触发...我按 F5,sql 查询运行,但在它运行之前,它将查询的副本保存到 B:\SSMS Queries\20130225\083000.sql.

这里缺少什么?我想为使用的连接/数据库添加选项,例如 B:\SSMS Queries\Localhost\Northwind\20130225\083000.sql(只是一个例子)。

我通常会做的事情...断点、单步执行、检查对象等...虽然这是一个插件。类库。您不能在库中设置断点/单步执行...

如何将断点放入加载到 SSMS/Visual Studio 的类库中以便我进行研究?或者什么是进行这种修补的好资源? object customin, object customout 中的某处是我想要修改的信息。

最佳答案

问题的第二部分找到与当前数据库的连接..

添加对 Microsoft.SqlServer.RegSrvrEnum.dll 和 SqlWorkBench.Interfaces 的引用(位于 C:\ProgramFiles..\SQL Server.. - 中的某处)。确保您已经为这些工具安装了 SDK。

那么下面的代码应该可以解决问题(欢迎您!)

IScriptFactory scriptFactory = ServiceCache.ScriptFactory;
CurrentlyActiveWndConnectionInfo connectionIfno = scriptFactory.CurrentlyActiveWndConnectionInfo;
UIConnectionInfo conn = connectionIfno.UIConnectionInfo;
Debug.WriteLine("{0}::{1}", conn.ServerName, conn.AdvancedOptions["DATABASE"]);

关于c# - SSMS 可扩展性项目 - 如何研究/调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15080185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com