Imports System.Data.Common
Imports Microsoft.VisualStudio.DebuggerVisualizers
Imports System.IO
' Apply an attribute to the assembly to indicate that it's recognized as a debugger visualizer
<Assembly: System.Diagnostics.DebuggerVisualizer(GetType(DBConnectionVisualizer), _
GetType(DebugeeObjectSource), _
Target:=GetType(DbConnection), _
Description:="BinaryRefinery DBConnection Visualizer")>
''' <summary>
''' Implements the debugger side of the Database Connection Visualizer
''' </summary>
''' <remarks>This class is used as a add-on for visual studio and
''' provides visualization capabilities to the debugger for objects that derive
''' from <see cref="System.Data.Common.DbConnection"/>
''' </remarks>
Public Class DBConnectionVisualizer
Inherits DialogDebuggerVisualizer
Implements IDBConnectionProxy
Private mProvider As IVisualizerObjectProvider
''' <summary>
''' Shows the user interface for the visualizer
''' </summary>
''' <param name="windowService">The object that provides windowing services</param>
''' <param name="objectProvider">The object that provides data</param>
Protected Overrides Sub Show(ByVal windowService As IDialogVisualizerService, _
ByVal objectProvider As IVisualizerObjectProvider)
' Show the UI form, passing it a reference to the object provider interface
mProvider = objectProvider
Dim ui As New QueryForm(Me)
' Show the form
windowService.ShowDialog(ui)
End Sub
#Region "IDBConnectionProxy Interface"
''' <summary>
''' Runs a query against the database connectioin
''' </summary>
''' <param name="sql">The SQL to run</param>
''' <returns>The resulting data table</returns>
Private Function RunQuery(ByVal sql As String) As QueryResult Implements IDBConnectionProxy.RunQuery
' Cast me to an object provider
Dim result As QueryResult
' Transfer the SQL statement to the debugee side
Dim sqlStream As IO.Stream = GetStreamFromString(sql)
mProvider.TransferData(sqlStream)
' Call the debugee side to give us the data
result = DirectCast(mProvider.GetObject(), QueryResult)
Return result
End Function
''' <summary>
''' Returns a stream containing the data for a string
''' </summary>
''' <param name="input">The string to write to the stream</param>
''' <returns>The stream containing the string data</returns>
Private Function GetStreamFromString(ByVal input As String) As Stream
' Use a string writer to write the string to a stream
Dim outputStream As New MemoryStream(Text.Encoding.ASCII.GetBytes(input))
' Return the stream
Return outputStream
End Function
#End Region
End Class