In the IDE of Visual Studio .NET, use interrupts to make program debugging, generally only simple local variables or expressions can be seen directly. If it is an object such as a DataSet, it is only a first-class level in the Locals window, and finally see a pile of scattered attribute values, most attributes are not large. Really useful is the result of the query of ADO.NET, if you can display DataTable in a DataSet variable, is it very attractive?
The Add-IN expansion mechanism provided by Visual Studio .NET provides us with your own debugging tool. Below is a simple example of using a DEBUGER object to display a local variable or expression result in the debugger.
First, implement the steps:
(1) Creating a VB Project of Add-in type
Creating the Add-in type Project can with the Wizard provided by VS.NET. This wizard is to do a CONNECT class and other functions such as OnConnection write basic code, where the core is to add a new command to the Tools menu to call up the command in the Add-in DLL.
The following is the code snippet of Connect.vb:
Public Class Connect
Implements extensibility.idtextensibility2
Implements IDTCommandTarget
DIM ApplicationObject as envdte.dte
Dim AddinInstance as envdte.addin
'In order to facilitate other parts to call objects in IDE, define a static variable DTE
Public Shared DTE as Envdte.dte 'Shared Among Other Modules
...
Public Sub OnConnection (ByVal application As Object, ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef custom As System.Array) Implements Extensibility.IDTExtensibility2.OnConnection
...
Try
Commandobj = ApplicationObject.commands.addnamedcommand (Objaddin, "Debughelper", "Debug Helper", "Executes The Command for Debughelper", True, 59, Nothing, 1 2)
'This is the function of adding a Tools menu to perform add-in features
Commandobj.AddControl (ApplicationObject.commandbars.Item ("Tools"))
Catch e as system.exception
Messagebox.show (e.tostring)
END TRY
END IF
End Sub
Public Sub Exec (Byval CmdName As String, Byval ExecuteOption As VscommandexecOption, Byref Varources As Object, Byref Handled As Boolean) IMPEments IDTCOMMANDTARGET.EXEC
Handled = false
IF (executeOption.vscommandexecoption.vscommandexecoptiondodefault) Thenif cmdname = "debughelper.connect.debughelper"
Handled = TRUE
'This function is written by yourself, used to call the main operation screen of Debughelper
Showmainform ()
EXIT SUB
END IF
END IF
End Sub
The function of the showMainform function is to display a debug operation screen. It features a DataSet variable name when debugging into the interrupt state, and then completes a DataTable in the DataSet in the DataGrid below via the showit button.
(2) The content of the entire DataSet variable from the debug environment
How to get a complete content of a target type variable from the debugger? The DEBUGER object provided by the Add-In programming interface has a getExpression method that converts a string into an Expression object, and the Value property of the Expression object returns the value of the expression.
For example, the following code can display the value of the variable name or expression input by the debugger:
Private _debugger as envdte.debugger
_debugger = connection.dte.debugger
DIM EXPR AS Envdte.expression
expr = _debugger.getexpression (txtexpression.text)
Messagebox.show (expr.value)
However, unfortunately, the Value property of the Expression object can only return a value of the simple data type (such as an integer, string, date, etc.), if you enter a DataSet variable, this value attribute returns only a string representing its type: {System.data.dataset}.
In order to solve this problem, we think of a whisper method: because the interface information in the DataSet is determined, use the Expression object to obtain a value of different expressions, then "fabricate" a Dataset for DataGrid, specific steps ( For example, the DataSet variable entered by the user is DS):
Take the value of the expression DS.DataSetName and create a new DataSet object accordingly
Take the value of the expression ds.tables.count to determine the number of times of a loop
Use the value of the expression DS.TABLES (i) .Columns.count to get the number of fields in each DataTable
Recycled the expression DS.Tables (i) .Columns (i) .ColumnName and .DataType, etc., DataSet's schema is determined.
With a similar approach, remove the value of DS.TABLES (i) .ROWS (J) (K), each field value of each record is got.
Finally, the newly created DataSet is bound to the DataGrid on the interface, and the user can see all the DATATABLE in the DS variable.
The following is the code for the two functions of the core function:
Private function buildDataSetByExpression (Byval Expr as envdte.expression) AS data.DataSet
'The Expr Should Be a Variable Such as DSData.
'The expression Object in Debugger Only Evaluation.' You cannot get the tables (0) from DSData Expression.
'This function uses oow expression.
'For example, in order to get the table count in the dataset
'It used the dsdata.tables.count expression.
Dim dsdata as data.dataset = new data.dataset
Try
If not isdataset (expr) THEN RETURN NOTING 'For in case the parameter is invalid
'Get the dataset name from variable / expression
DSData.DataSetName = evr.name ".datasetName")
'Get the number of datatables in the dataset
DIM TABLECUNT AS INTEGER = CTYPE (EvaluateExpression (expr.name ".tables.count") "), Integer 'The return value from the function is a string so ctype ...
'Get the datatables with a loop
DIM I as integer
DIM J AS INTEGER
DIM K As Integer
Dim dtable as data.dataable
DIM columncount as integer
DIM DCOLUMN AS DATA.Datacolumn
DIM ROWCOUNT AS INTEGER
DIM ITEMARRAY () AS Object
For i = 0 to TableCount - 1
'Create a New DataTable
DTABLE = New DataTable
'Get the table name with expression
DIM TABLENAME AS STRING = evr.name ".Tables (" & I & ") .tablename")
DTable.TableName () = Tablename.substring (1, Tablename.length - 2) 'Remove The Double Quotation Marks
'Get the column country from the variable
Columncount = ctype (EvaluateExpression (expr.name ".tables (" & I & ") .columns.count"), Integer
'Add the columns to dataatable object
For j = 0 to columncount - 1
DCOLUMN = New Datacolumn
Dim columnName As String = EvaluateExpression (expr.Name ".Tables (" & I & ") .Columns (" & J & ") .ColumnName") dColumn.ColumnName = columnName.Substring (1, columnName.Length - 2)
Dim Dattypename As String = evr.name ". Tables (" & I & ") .COLUMNS (" & J & ") .DATATYPE.NAME")
'The return value incrudes 2 Double Quotation Marks, String -> "String"
DataTypename = Datatypename.substring (1, DataTypename.length - 2)
DCOLUMN.DATATYPE = type.gettype ("System." & Datatypename, true, true) 'gettype Needs the full name of type
DTable.Columns.Add (DCOLUMN)
NEXT
'Get the row count from the variable
RowCount = ctype (EvaluateExpression (expr.name ".tables (" & I & ") .rows.count"), integer
'Add the rows of this table
Fork = 0 TO ROWCOUNT - 1
'Get the itemarray of each column
Redim ItemArray (ColumnCount - 1) 'Clear the Old Contents
For j = 0 to columncount - 1
'The expression used to get the value of the current color column in current row
DIM columnValueExpression as string = expr.name ".tables (" & I & ") .ROWS (" & K & ") .ItemRay (" & J & ") .tostring"
DIM VALUESTRING AS STRING = EvaluateExpression (ColumnValueExpression)
Valuestring = valueString.substring (1, valueString.Length - 2)
If not valueString = ""hen 'if the colorn value is system.dbnull
ItemArray (j) = valueString
END IF
NEXT
'Add the new row to the rows
DTable.Rows.Add (itemaray)
NEXT
'Add the table into the dataset
DSData.Tables.Add (DTABLE) Next
Catch exception
Messagebox.show (ex. Tnowtring, "Debug Helper")
END TRY
Return DSData
END FUNCTION
Private function evataExpression (Byval Expression As String) AS STRING
Try
DIM Anyexpression as envdte.expression
AnyExpression = _debugger.getexpression (Expression)
Return Anyexpression.Value 'it is a string !!!
Catch exception
Return "" Return an Empty String On Error
END TRY
END FUNCTION
Second, add-in usage:
The method of using this Add-in program is simple:
(1) Installation
When you create add-in project, Wizard will help build a supporting Project for making the SETUP program. Build This Setup Project, you can get a .msi file.
Run this .msi file, then open the visual studio.net IDE, you will see more than a debughelper command under the Tools menu.
(2) Turn out other Solution, debug into the interrupt.
(3) Select the debughelper command in the Tools menu to call up the operation screen.
(4) Enter a DataSet variable name, then click the showit button, the first DataTable in the DataSet is displayed in the table, selecting the other DataTable in ComboBox to see the data in other results tables.
(5) If there is no debughelper command in the Tools menu, you can rebuild the command with a Wizard generated recreateCommands.reg file, which is as follows:
Regedit4
[HKEY_CURRENT_USER / SOFTWARE / Microsoft / VSA / 7.1 / preloadaddInstate]
"Debughelper.connect" = dword: 1
[HKEY_CURRENT_USER / SOFTWARE / Microsoft / Visualstudio / 7.1 / preloadaddInState]
"Debughelper.connect" = dword: 1
Note: This program writes using Visual Studio.net 2003, but there should be no problem on Visual Studio.net 2002.