How To : Create SQL Server Management Studio Addin

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
简介: 原文 How To : Create SQL Server Management Studio Addin Read the full and original article from Jon Sayce Here In the last post I talked about How To:...

原文 How To : Create SQL Server Management Studio Addin

Read the full and original article from Jon Sayce Here

In the last post I talked about How To: Create Windows Live Messenger Addin

Now let’s create SQL Server Management Studio Addin.

Start:

Let’s open Visual Studio and create a new Visual Studio Add-in project.

Step1

Check the right options for your Addin.

step3

After you finish the wizard please add new Setup project.

step4

Add Project output  -> Primary output and change the output group registration to vsdrpCOM

step5

Enter to Registry Editor and add your Addin to SSMS startup.

step6 

 

References

There’s extensive documentation on MSDN regarding Visual Studio’s EnvDTE object model, which is at the heart of Visual Studio add-in development, and most of this applies to SSMS. Most UI elements are the same for both environments but if your add-in relates to anything SQL-specific then you’re going to need references to SSMS assemblies and the documentation on these is non-existent.

IDTExtensibility2 Events

Once you’ve got the references sorted out, you’re ready to start coding.

The template class that Visual Studio has created implements the IDTExtensibility2 interface, but only one of the methods has any code in it so far: OnConnection.

OnConnection “occurs whenever an add-in is loaded into Visual Studio” according to MSDN – in our case the event will fire whenever you start SSMS, once the add-in is installed.

OnConnection will probably be the most important method of the interface for your add-in, but the others can be useful if you need to save settings as the add-in is unloaded or something similar.

SSMS Events

Handling SSMS events is one of the trickier aspects of writing the add-in, the problem being that you don’t know what events there are to handle. I suspect Reflector could help here, but the method I used was suggested by Sean.

To add a handler to an event we need to know the command to which that event belongs. The easiest way to find this is to loop through the commands and look for the name which sounds most like what you’re after.

For Each com As Command In _DTE.Commands
   Debug.WriteLine(String.Format("Name={0} | GUID={1} | ID={2}", com.Name, com.Guid, com.ID)) Next

Now we complete our Addin infrastructure we can start writing some code!!!

Edit Connect.vb and add a MessageBox inside OnStartupComplete method.

image

Build the project and install the Addin, open SSMS and this is what you should see.

step7

Add New Menu Item

Under Connect Class change to :
Implements the constructor for the Add-in object

Implements IDTExtensibility2
Implements IDTCommandTarget
 
Private _DTE2 As DTE2
Private _DTE As DTE
Private _addInInstance As AddIn
 
Private _CommandEvents As CommandEvents
 
Private _CommandBarControl As CommandBarControl
 
Private Const COMMAND_NAME As String = "MySSMSAddinCommand"

OnConnection Method:
get the events for the command we’re interested in (the GUID comes from the output of the previous debug command)
 NOTE: if the _CommandEvents object goes out of scope then the handler will not longer be attached to the event, so it must be a private class-level declaration rather than a local one.

Public Sub OnConnection(ByVal application As Object, ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, ByRef custom As Array) Implements IDTExtensibility2.OnConnection
       _DTE2 = CType(application, DTE2)
       _DTE = CType(application, DTE)
       _addInInstance = CType(addInInst, AddIn)
 
       _CommandEvents = _DTE.Events.CommandEvents("{84125960-B63C-3794-B5D3-9BC47A513E8D}", 1)
   End Sub

OnDisconnection Method: - 
Checks whether the control in the tools menu is there if so delete the menu item.

Public Sub OnDisconnection(ByVal disconnectMode As ext_DisconnectMode, ByRef custom As Array) Implements IDTExtensibility2.OnDisconnection
        Try
            If Not (_CommandBarControl Is Nothing) Then
                _CommandBarControl.Delete()
            End If
        Catch
        End Try
    End Sub

QueryStatus Method:
called when the command’s availability is updated

Public Sub QueryStatus(ByVal commandName As String, ByVal neededText As vsCommandStatusTextWanted, ByRef status As vsCommandStatus, ByRef commandText As Object) Implements IDTCommandTarget.QueryStatus
       If neededText = vsCommandStatusTextWanted.vsCommandStatusTextWantedNone Then
           If commandName = _addInInstance.ProgID & "." & COMMAND_NAME Then
               status = CType(vsCommandStatus.vsCommandStatusEnabled + vsCommandStatus.vsCommandStatusSupported, vsCommandStatus)
           Else
               status = vsCommandStatus.vsCommandStatusUnsupported
           End If
       End If
   End Sub

OnStartupComplete Method:

Public Sub OnStartupComplete(ByRef custom As Array) Implements IDTExtensibility2.OnStartupComplete
        Dim myCommand As Command = Nothing
 
        ' -----------------------------------
        ' 1. Check whether the command exists
        ' -----------------------------------
 
        ' try to retrieve the command, in case it was already created
        Try
            myCommand = _DTE.Commands.Item(_addInInstance.ProgID & "." & COMMAND_NAME)
        Catch
            ' this just means the command wasn't found
        End Try
 
        ' ----------------------------------
        ' 2. Create the command if necessary
        ' ----------------------------------
 
        If myCommand Is Nothing Then
            myCommand = _DTE.Commands.AddNamedCommand(_addInInstance, COMMAND_NAME, "MySSMSAddin MenuItem", "Tooltip for your command", True, 0, Nothing, vsCommandStatus.vsCommandStatusSupported Or vsCommandStatus.vsCommandStatusEnabled)
        End If
 
        ' ------------------------------------------------------------------------------------
        ' 3. Get the name of the tools menu (may not be called "Tools" if we're not in English
        ' ------------------------------------------------------------------------------------
 
        Dim toolsMenuName As String
        Try
 
            ' If you would like to move the command to a different menu, change the word "Tools" to the 
            ' English version of the menu. This code will take the culture, append on the name of the menu
            ' then add the command to that menu. You can find a list of all the top-level menus in the file
            ' CommandBar.resx.
            Dim resourceManager As System.Resources.ResourceManager = New System.Resources.ResourceManager("MySSMSAddin.CommandBar", System.Reflection.Assembly.GetExecutingAssembly())
 
            Dim cultureInfo As System.Globalization.CultureInfo = New System.Globalization.CultureInfo(_DTE2.LocaleID)
            toolsMenuName = resourceManager.GetString(String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools"))
 
        Catch e As Exception
            'We tried to find a localized version of the word Tools, but one was not found.
            '  Default to the en-US word, which may work for the current culture.
            toolsMenuName = "Tools"
        End Try
 
        ' ---------------------
        ' 4. Get the Tools menu
        ' ---------------------
 
        Dim commandBars As CommandBars = DirectCast(_DTE.CommandBars, CommandBars)
        Dim toolsCommandBar As CommandBar = commandBars.Item(toolsMenuName)
 
        ' -------------------------------------------------
        ' 5. Create the command bar control for the command
        ' -------------------------------------------------
 
        Try
            'Find the appropriate command bar on the MenuBar command bar:
            _CommandBarControl = DirectCast(myCommand.AddControl(toolsCommandBar, toolsCommandBar.Controls.Count + 1), CommandBarControl)
            _CommandBarControl.Caption = "MySSMSAddin"
        Catch argumentException As System.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.
        End Try
    End Sub

 

Build and Install

step8

Add New Window

Let’s make our menu item a functioning menu item. 
First add new UserControl to the project

image

Modify the UserControl to your needs.

Add this code into Exec Method in Connect.vb
Define a new window as container for the UserControl.

Public Sub Exec(ByVal commandName As String, ByVal executeOption As vsCommandExecOption, ByRef varIn As Object, ByRef varOut As Object, ByRef handled As Boolean) Implements IDTCommandTarget.Exec
    handled = False
    If executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault Then
        If commandName = _addInInstance.ProgID & "." & COMMAND_NAME Then
 
            ' get windows2 interface
            Dim MyWindow As Windows2 = CType(_DTE2.Windows, Windows2)
 
            ' get current assembly
            Dim asm As Assembly = System.Reflection.Assembly.GetExecutingAssembly
 
            ' create the window
            Dim MyControl As Object = Nothing
            Dim toolWindow As Window = MyWindow.CreateToolWindow2(_addInInstance, asm.Location, "MySSMSAddin.MyAddinWindow", "MySMSAddin Window", "{5B7F8C1C-65B9-2aca-1Ac3-12AcBbAF21d5}", MyControl)
            toolWindow.Visible = True
 
            handled = True
 
        End If
    End If
End Sub

 

image

image 

Download MySSMSAddin Project

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
2月前
|
SQL 数据库 数据安全/隐私保护
【操作宝典】SQL巨擘:掌握SQL Server Management的终极秘籍!
【操作宝典】SQL巨擘:掌握SQL Server Management的终极秘籍!
71 0
|
9天前
|
SQL 供应链 安全
Hospital Management Startup 1.0 SQL 注入(CVE-2022-23366)
Hospital Management Startup 1.0 SQL 注入(CVE-2022-23366)
|
9天前
|
SQL 安全 网络安全
Hospital Management System v4.0 SQL 注入(CVE-2022-24263)
Hospital Management System v4.0 SQL 注入(CVE-2022-24263)
|
2月前
|
SQL Shell 数据库
无人参与安装最新版本SQL Server Management Studio (SSMS)
通过power shell完成SSMS的自动安装,给大家争取打水的时间,离开位置走走,活动活动筋骨。
74 0
|
SQL IDE 开发工具
【SQL Server Management 2019下载及安装】
【SQL Server Management 2019下载及安装】
182 0
【SQL Server Management 2019下载及安装】
|
SQL
SQL Server Management Studio登录框中的“服务器名”填写
SQL Server Management Studio登录框中的“服务器名”填写
238 0
|
SQL IDE 开发工具
SQL Server manager studio(SSMS)的安装教程
1、SQL Server manager studio(SSMS)安装包下载 方法一:选择从官网下载: https://learn.microsoft.com/en-us/sql/ssms/sql-server-management-studio-ssms?view=sql-server-ver16 点击free download即可下载 方法二:百度网盘下载: 如果觉得上面方法下载慢的话也可以通过我的网盘进行下载 网盘链接: https://pan.baidu.com/s/1vi9sEH
SQL Server manager studio(SSMS)的安装教程
sbs
|
SQL 存储 监控
One SQL to Rule Them All: An Efficient and Syntactically Idiomatic Approach to Management of Streams and Tables 论文翻译
One SQL to Rule Them All: An Efficient and Syntactically Idiomatic Approach to Management of Streams and Tables[文件: One SQL to Rule Them All- An Efficient and Syntactically Idiomatic Approach to Manag
sbs
177 0
One SQL to Rule Them All: An Efficient and Syntactically Idiomatic Approach to Management of Streams and Tables 论文翻译
|
SQL JavaScript 关系型数据库
iot studio业务逻辑通过Nodejs脚本实现较为复杂的SQL语句操作RDS数据库
业务逻辑提供了云数据库MySQL节点,使用云数据库MySQL节点可将数据存入云数据库RDS MySQL版数据表中,但仅支持对数据表中的数据进行简单的增、删、改、查操作。可以通过Nodejs脚本实现连接数据库,以SQL语句的形式操作数据库。
341 1
iot studio业务逻辑通过Nodejs脚本实现较为复杂的SQL语句操作RDS数据库
|
SQL Web App开发
一个问题:CDS view在HANA studio里执行,显示的耗时比在ABAP open SQL里少
一个问题:CDS view在HANA studio里执行,显示的耗时比在ABAP open SQL里少
108 0
一个问题:CDS view在HANA studio里执行,显示的耗时比在ABAP open SQL里少