How to:create SQL Server Management Studio Addin

Source: Internet
Author: User
Tags object model management studio sql server management sql server management studio

Original how to:create SQL Server Management Studio Addin

Read the full and original article from Jon Sayce here

In the Last Post I talked on 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.

Check the right options for your Addin.

After your finish the wizard please add New Setup project.

Add Project output, Primary output and change, the output group registration to vsdrpcom

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

References

There ' s extensive documentation on MSDN regarding visual Studio's EnvDTE object model, which is at the heart of visual Stu Dio add-in Development, and most of the this applies to SSMS. Most UI elements is 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 are non-existent.

IDTExtensibility2 Events

Once you ' ve got the references sorted out and you're ready to start coding.

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

OnConnection "occurs whenever an add-in was loaded into Visual Studio" according to Msdn–in we case the event would fire Whenever you start in SSMS, once the add-in is installed.

OnConnection would probably be the most important method of the interface for your add-in, but the others can 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 so you don ' t know what even TS there is to handle. I suspect Reflector could help here, but the method I used is suggested by Sean.

To add a handler to an event we need to know the command to which that event belongs. The easiest-on-the-find-is-to-loop through the commands and look for the name which sounds of the most "What do you ' re afte R.

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.

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

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 would not longer being attached to the event, so it must is 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, Byre F 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 was there if so delete the menu item.

Public Sub OnDisconnection (ByVal disconnectmode as Ext_disconnectmode, ByRef Custom as Array) Implements idtextensibility 2.OnDisconnection
        Try
            If not (_commandbarcontrol are 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 Vscomma Ndstatus, 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 is not being called "tools" if we ' re not in 中文版
        ‘ ------------------------------------------------------------------------------------
        Dim toolsMenuName as String
        Try
            
            ' 中文版 version of the menu. This code would take the culture, append 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 is 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 and then the exception is probably because a command with that name
            '  safely ignore the exception.
        End Try
    End Sub

Build and Install

ADD New Window

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

Modify the UserControl to your needs.

ADD This code to 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 Varo UT 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

Download Myssmsaddin Project

How to:create SQL Server Management Studio Addin

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.