编写SQL Server Management Studio插件2

How To : Create SQL ServerManagement Studio Addin

from: http://blogs.microsoft.co.il/blogs/shair/archive/2008/07/28/how-to-create-sql-server-management-studio-addin.aspx 

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 tovsdrpCOM

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
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: SQL Server Management StudioSSMS)是Microsoft SQL Server的集成环境。它旨在为数据库管理员、数据库开发人员和BI专业人士提供一种可视化的管理数据库的方式。SSMS提供了许多功能,用于SQL Server的资源管理、备份和还原数据库、创建和调试存储过程、查询和分析数据等。 SSMS的最新版本是SQL Server Management Studio 18,但SSMS 2008 R2仍然广泛使用。SSMS 2008 R2可以与SQL Server 2008 R2数据库服务器一起使用。它提供了与SQL Server数据库的连接,使您可以对数据库的对象进行管理。 SSMS 2008 R2具有以下功能: 1. 对象资源管理器:用于管理SQL Server实例和其中的对象,例如数据库、表、视图和存储过程。 2. 查询分析器:用于编写和运行SQL查询,并将结果呈现为表格、图表或文本。 3. 查询设计器:用于创建和修改SQL查询。 4. 作业管理器:用于创建、调度和监视作业,例如备份和定期执行的任务。 5. 数据库引擎调试器:用于调试存储过程和触发器。 6. 数据库引擎优化顾问:用于分析数据库和存储过程的性能,并提供优化建议。 总之,SSMS 2008 R2是一个强大的工具,用于管理和开发SQL Server数据库。虽然它有一些限制,但它仍然是广泛使用的工具之一,特别是在企业中使用较旧版本的SQL Server时。 ### 回答2: SQL Server Management Studio 2008 R2是一款强大的数据库管理工具,可用于管理和维护Microsoft SQL Server数据库。它提供了丰富的功能,包括数据库设计、管理、备份和还原、脚本编写和调试、性能监视和分析等等。 该工具的用户界面直观友好,使用户可以快速轻松地完成各种管理任务。它还支持多种语言,包括Transact-SQL、VB、C#等,可以满足不同类型用户的需求。 SQL Server Management Studio 2008 R2还能够与其他Microsoft产品进行无缝协作,例如Visual StudioSharePoint和Excel等等。这意味着用户可以利用这些产品的功能来访问和处理数据库。 此外,SQL Server Management Studio 2008 R2具有高度可扩展性,可以轻松添加新功能和插件。这使得它可以适应不同规模和类型的系统,并满足不同组织和用户的需求。 综上所述,SQL Server Management Studio 2008 R2是一个强大的数据库管理工具,可以帮助用户轻松地管理和维护Microsoft SQL Server数据库,并具有高度可扩展性和与其他Microsoft产品的无缝协作。 ### 回答3: SQL Server Management Studio(简称SSMS)是一款由微软提供的用于管理和操作SQL Server数据库的应用程序。SSMS 2008 R2是一个比较旧的版本,发布于2010年,但仍然是许多企业和组织使用的标准数据库管理工具之一。 SSMS 2008 R2在功能上比较基础,但它仍然包括了许多主要的功能,如数据库创建和维护、查询编写和执行、对象管理、数据导入和导出、安全性和用户权限设置等等。 另外,SSMS 2008 R2与许多其他Microsoft工具集成在一起,如Visual Studio和Azure Data Studio。这使得开发人员和数据库管理员可以更方便地使用其他工具和应用程序来管理和操作数据库。 尽管SSMS 2008 R2是一个老版本,但它仍然是一个可靠的工具,对于许多组织来说仍然是一个必不可少的资源。但是,随着SQL Server和其他数据库管理系统的发展,新版本的SSMS可以提供更多的功能和更简单易用的界面,同时还包括更好的性能和安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值