How To : Create SQL Server Management Studio Addin
原文 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.
Check the right options for your Addin.
After you 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 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.
Build the project and install the Addin, open SSMS and this is 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 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
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 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
Download MySSMSAddin Project
How To : Create SQL Server Management Studio Addin的更多相关文章
- Sql Server系列:Microsoft SQL Server Management Studio模板资源管理器
模板资源管理器是Microsoft SQL Server Management Studio的一个组件,可以用来SQL代码模板,使用模板提供的代码,省去每次都要输入基本代码的工作. 使用模板资源管理器 ...
- [Windows Azure] Managing SQL Database using SQL Server Management Studio
Managing Windows Azure SQL Database using SQL Server Management Studio You can use Windows Azure SQL ...
- 開啟活動監視器 (SQL Server Management Studio)
本主題描述如何開啟 [活動監視器] 來取得有關 SQL Server 處理序以及這些處理序如何影響目前 SQL Server 執行個體的資訊. 此外,本主題也描述如何設定 [活動監視器] 的重新整理間 ...
- SQL Server Management Studio自定义快捷键
SQL Server Management Studio支持自定义快捷键:工具->选项->键盘: 其中,Alt+F1.Ctrl+1.Ctrl+2是系统预定义的快捷键. 双击表名(或按Ctr ...
- 使用PD(Power Designer)设计数据库,并且生成可执行的SQL文件创建数据库(本文以SQL Server Management Studio软件执行为例)
下载和安装PD: 分享我的软件资源,里面包含了对PD汉化包(链接出问题时可以留言,汉化包只能对软件里面部分菜单栏汉化) 链接:https://pan.baidu.com/s/1lNt1UGZhtDV8 ...
- SQL Server Management Studio 无法修改表,超时时间已到 在操作完成之前超时时
在修改表时,保存的时候显示:无法修改表,超时时间已到 在操作完成之前超时时间已过或服务器未响应 这是执行时间设置过短的原因,可以修改一下设置便能把执行时间加长,以便有足够的时间执行完修改动作. 在 S ...
- 禁用SQL Server Management Studio的IntelliSense
禁用SQL Server Management Studio的IntelliSense 本文版权归作者所有,未经作者同意不得转载.
- 如何清除SQL Server Management Studio的最近服务器列表
SQL Server Management Studio (SSMS) 的"连接到服务器"对话框会记录用户所有访问过的服务器名称,这个功能对于经常连接多个数据库的人来说确实挺方便的 ...
- SQL Server Management Studio 2012 设置脚本默认保存路径
特别说明,本文是从这里 修改SQL Server Management Studio默认设置提高开发效率. "抄过来的",为方便个人记忆才写此文(非常感谢这哥们儿的分享.) 原文地 ...
随机推荐
- 定制XP引导屏幕背景图像和替换windows这句话
(一)带滚动条界面变化 1.制作640*480位图,4位置16肤色,尺寸小于200Kb. 2.位图名boot.bmp,并将其复制到C:/windows根文件夹 3.更改boot.ini档.我的电脑/属 ...
- M、V、C
概述 Model-View-Controller(MVC),即模型-视图-控制器. MVC将软件系统分成三大部分:Model,View,Controller,三个部分通过某种机制通信 M.V.C的职能 ...
- MVC中使用SignalR
MVC中使用SignalR打造酷炫实用的即时通讯功能附源码 前言,现在这世道写篇帖子没个前言真不好意思发出来.本贴的主要内容来自于本人在之前项目中所开发的一个小功能,用于OA中的即时通讯.由于当时 ...
- Vim 命令 【转】
高级一些的编辑器,都会包含宏功能,vim当然不能缺少了,在vim中使用宏是非常方便的: :qx 开始记录宏,并将结果存入寄存器xq 退出记录模式@x 播放记录在x寄存器中的宏命 ...
- redis安装和配置教程phpredis扩展安装测试
作者:zhanhailiang 日期:2014-10-16 推荐阅读: Redis持久化策略 关于Redis很多其它资料阅读 1. 下载redis-2.8.17.tar.gz:http://downl ...
- PHP Yii框架开发——组织架构网站重构
最近一段时间在维护公司的组织架构网站(Org),旧版网站只是用了xampp简单搭建了一套环境部署在了windows机器上,代码结构相对简单. 整个架构如下: 整个架构没有用到复杂的结构,class里放 ...
- iptables的CLUSTER target以太网交换机和想法
周末热风,这个想法从未在我的心脏像样的雨一阵悲哀. 每到周末,我会抽出一周整夜的事情的总结,无论是工作.人生,或者在上班或在锯的方式方法,并听取了抑制书评,因为无雨,周六晚上,我决定好好睡一觉,再折腾 ...
- http://blog.jobbole.com/50603/#comment-153933
http://blog.jobbole.com/50603/#comment-153933
- [Error]EOL while scanning string literal
有一个经常性的工作项目.需要一天的一些表数据到外部接口,但最近总是异常.今天检查的原因. 第一本地和测试环境中测试程序是没有问题,有网络环境只会在日志中抛出一个异常.产生主要的例外是推定异常数据. , ...
- 分析RAC下一个SPFILE整合的三篇文章的文件更改
大约RAC下一个spfile分析_整理在_2014.4.17 说明:文章来源于网络 第一篇:RAC下SPFILE文件改动 在RAC下spfile位置的改动与单节点环境不全然一致,有些地方须要特别注意, ...