Table of Contents [Hide/Show]
Introduction Basic Concept Assemblies for SQL Server Management Studio Implementing IDTExtensibility2 OnConnection Declarations Declaring SQLObjectNode Context Menu Addition Class GetInstanceOnSelection Getting the Selected Object Node SQLObjectNode Constructor Adding a Menu Item The Menu class to perform actions Invoke Method Copying the contents to New Query Window Creating a New Query Window Complete Code Debugging Registry Setup Package
Figure 1
Figure 2
Figure 3
Figure 4
Figure 5
Figure 6
Figure 7
Connect
IDTEExtensibility2
EnvDTE
EnvDTE80
Extensibility
C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies\
C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\
Microsoft.sqlServer.ConnectionInfo
Microsoft.sqlServer.RegSvrEnum
objectexplorer
Microsoft.sqlserver.sqltools.VSIntegration
sqlWorkbench.Interfaces
IDTExtensibility2
OnConnection
Application
connectMode
addInInst
OnDisconnection
disconnectMode
Custom
OnAddInsUpdate
OnStartupComplete
OnBeginShutdown
OnSelectionChanged
IObjectExplorerEventProvider
SelectionChanged
_ApplicationObject = Ctype(Application, DTE2) _addInInstance = Ctype(AddINInst, AddIn) _ObjectExplorer = ServiceCache.GetObjectExplorer Dim typeOfProvider as Type = GetType(IObjectExplorerEventProvider) Dim provider as IObjectExplorerEventProvider = _ Ctype(ObjectExplorer.GetService(typeOfProvider), IObjectExplorerEventProvider) AddHandler provider.SelectionChanged, AddressOf OnSelectionChanged
_ApplicationObject
_addInINstance
_ObjectExplorer
Private _ApplicationObject as DTE2 Friend _addINInstance as AddIn Private _ObjectExplorer as IObjectExplorerService
Friend
SQLObjectNode
Dim objSQLDBNode as SQLObjectNode = SQLObjectNode.GetInstanceOnSelection(Me)
GetInstanceOnSelection
Friend Shared Function GetInstanceOnSelection( _ ByVal objSQLServer2005AddIn As SQLServer2005AddIn) As SQLObjectNode Dim node As INodeInformation Dim sz As Int16 Dim selNodes() As INodeInformation objSQLServer2005AddIn.ObjectExplorer.GetSelectedNodes(sz, selNodes) If sz > 0 Then node = selNodes(0) If Not node Is Nothing Then Dim context As String = node.Context Dim tnode As SQLObjectNode If (objSQLServer2005AddIn._objNodesDict.ContainsKey(context)) Then tnode = objSQLServer2005AddIn._objNodesDict(context) Else tnode = New SQLObjectNode(node, objSQLServer2005AddIn) objSQLServer2005AddIn._objNodesDict.Add(context, tnode) End If Return tnode End If End If End If Return Nothing End Function
objSQLServer2005AddIn.ObjectExplorer.GetSelectedNodes(sz, selNodes)
SelNodes
Microsoft.SQLServer.Management.UI.VSIntegration.ObjectExplorer.INodeInformation
selNodes
tnode = New SQLObjectNode(node, objSQLServer2005AddIn)
Private Sub New(ByVal node As INodeInformation, _ ByVal objSQLServer2005AddIn As SQLServer2005AddIn) Me._objSQLServer2005AddIn = objSQLServer2005AddIn If node Is Nothing Then _isTable = False Else Dim context As String = node.Context If context.Contains("Server[@Name='") AndAlso _ context.Contains("']/Database[@Name='") AndAlso _ context.Contains("/Table[@Name") AndAlso _ context.Contains("@Schema") AndAlso _ node("Name") IsNot Nothing AndAlso _ node("Schema") IsNot Nothing AndAlso _ node("Name").ToString().Trim() <> "" AndAlso _ node("Schema").ToString().Trim() <> "" Then _isTable = True _isDB = False ElseIf context.Contains("Server[@Name='") AndAlso _ context.Contains("']/Database[@Name='") AndAlso _ node("Name") IsNot Nothing AndAlso _ node("Name").ToString().Trim() <> "" Then _isDB = True _isTable = False End If End If If _isTable Then _name = node("Name").ToString() _schema = node("Schema").ToString() _database = node.Parent.Name _cnnStr = "Database=" + database + ";" + node.Connection.ConnectionString If Not objSQLServer2005AddIn.TableMenuRegistered Then AddMenuItems(CType(node.GetService(GetType(IMenuHandler)), _ IMenuHandler), True) objSQLServer2005AddIn.TableMenuRegistered = True End If ElseIf _isDB Then _name = node("Name").ToString _database = _name _cnnStr = "Database=" + database + ";" + node.Connection.ConnectionString If Not objSQLServer2005AddIn.DBMenuRegistered Then AddMenuItems(CType(node.GetService(GetType(IMenuHandler)), _ IMenuHandler), False) objSQLServer2005AddIn.DBMenuRegistered = True End If End If End Sub
node.context
"Server[@Name='ETGPHADNIS\SQL2005']/Database[@Name='SQLReportApplication']"
"Server[@Name='ETGPHADNIS\SQL2005']/Database[@Name='SQLReportApplication']/Table[@Name='tblApplication' and @Schema='dbo']"
tblApplication
SQLReportApplication
Dim objMenuItem As New SPGeneratorMenu(objSQLServer2005AddIn) objMenuItem.Text = strItemName Try Dim testing As HierarchyObject = _ DirectCast(menuItemHandler, HierarchyObject) testing.AddChild(strItemName, objMenuItem) Catch ex As Exception Throw ex End Try
Text
ToolsMenuItemBase
SPGeneratorMenu
Invoke
Dim tnode As SQLObjectNode = _ SQLObjectNode.GetInstanceOnSelectedNode(_objSQLServer2005AddIn) If Not tnode Is Nothing Then Dim frmGenerator As frmSPGenerator Try If tnode.IsDB Then frmGenerator = New frmSPGenerator(tnode.cnnStr, tnode.database, _ False, _objSQLServer2005AddIn) Else frmGenerator = New frmSPGenerator(tnode.cnnStr, tnode.Name, _ True, _objSQLServer2005AddIn) End If frmGenerator.ShowDialog() Catch ex As Exception MessageBox.Show(ex.Message) Finally frmGenerator = Nothing If Not sw Is Nothing Then sw.Dispose() sw = Nothing End If End Try End If
frmSPGenerator
Dim scriptFactory As Editors.IScriptFactory = ServiceCache.ScriptFactory scriptFactory.CreateNewBlankScript(Editors.ScriptType.Sql, _ scriptFactory.CurrentlyActiveWndConnectionInfo.UIConnectionInfo, Nothing)
EnvDTE.TextDocument
Dim objEnvDocument As EnvDTE.Document Dim objTextDocument As EnvDTE.TextDocument Dim objEndPoint As EnvDTE.EditPoint Dim objDBProcedures As New DBProcedures Dim scriptFactory As Editors.IScriptFactory = ServiceCache.ScriptFactory scriptFactory.CreateNewBlankScript(Editors.ScriptType.Sql, _ scriptFactory.CurrentlyActiveWndConnectionInfo.UIConnectionInfo, Nothing) objEnvDocument = _objSQLServer2005AddIn.ApplicationObject.ActiveDocument objTextDocument = objEnvDocument.Object() objEndPoint = objTextDocument.EndPoint.CreateEditPoint objEndPoint.Insert(objDBProcedures.GetCopyrightComments) objEndPoint.Insert(objDBProcedures.GetDisclaimer) objEnvDocument = _objSQLServer2005AddIn.ApplicationObject.ActiveDocument objTextDocument = objEnvDocument.Object() objEndPoint = objTextDocument.EndPoint.CreateEditPoint objEndPoint.Insert(strProcedure.ToString)
Insert
EditPoint
C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\SqlWb.exe
Figure 8
HKEY_LOCAL_MACHINE \SOFTWARE \Microsoft \Microsoft SQL Server \90 \Tools \Shell \Addins \<your Add-in class>
Figure 9