I needed to find out exactly what was being submitted by DataContext.SubmitChanges and didn't want to use SQL Profiler.
I ran across this post by Kris Vandermotten and have converted the code to VB for anyone else who may need it.
This is now always in my Utility.vb file that gets attached to most of my projects.
Imports System.Diagnostics
Imports System.Globalization
Imports System.IO
Imports System.Text
''' <summary>
''' Implements a <see cref="TextWriter"/> for writing information to the debugger log.
''' </summary>
''' <seealso cref="Debugger.Log"/>
Public Class DebuggerWriter
Inherits TextWriter
Private _isOpen As Boolean
Private Shared _encoding As UnicodeEncoding
Private ReadOnly _level As Integer
Private ReadOnly _category As String
''' <summary>
''' Initializes a new instance of the <see cref="DebuggerWriter"/> class.
''' </summary>
Public Sub New()
Me.New(0, Debugger.DefaultCategory)
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="DebuggerWriter"/> class with the specified level and category.
''' </summary>
''' <param name="level">A description of the importance of the messages.</param>
''' <param name="category">The category of the messages.</param>
Public Sub New(ByVal level As Integer, ByVal category As String)
Me.New(level, category, CultureInfo.CurrentCulture)
End Sub
''' <summary>
''' Initializes a new instance of the <see cref="DebuggerWriter"/> class with the specified level, category and format provider.
''' </summary>
''' <param name="level">A description of the importance of the messages.</param>
''' <param name="category">The category of the messages.</param>
''' <param name="formatProvider">An <see cref="IFormatProvider"/> object that controls formatting.</param>
Public Sub New(ByVal level As Integer, ByVal category As String, ByVal formatProvider As IFormatProvider)
MyBase.New(formatProvider)
Me._level = level
Me._category = category
Me._isOpen = True
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
_isOpen = False
MyBase.Dispose(disposing)
End Sub
Public Overloads Overrides Sub Write(ByVal value As Char)
If Not _isOpen Then
Throw New ObjectDisposedException(Nothing)
End If
Debugger.Log(level, category, value.ToString())
End Sub
Public Overloads Overrides Sub Write(ByVal value As String)
If Not _isOpen Then
Throw New ObjectDisposedException(Nothing)
End If
If value <> Nothing Then
Debugger.Log(level, category, value)
End If
End Sub
Public Overloads Overrides Sub Write(ByVal buffer As Char(), ByVal index As Integer, ByVal count As Integer)
If Not _isOpen Then
Throw New ObjectDisposedException(Nothing)
End If
If buffer = Nothing OrElse index < 0 OrElse count < 0 OrElse buffer.Length - index < count Then
' delegate throw exception to base class
MyBase.Write(buffer, index, count)
End If
Debugger.Log(level, category, New String(buffer, index, count))
End Sub
Public Overloads Overrides ReadOnly Property Encoding() As Encoding
Get
If _encoding Is Nothing Then
_encoding = New UnicodeEncoding(False, False)
End If
Return _encoding
End Get
End Property
Public ReadOnly Property Level() As Integer
Get
Return Level
End Get
End Property
Public ReadOnly Property Category() As String
Get
Return _category
End Get
End Property
End Class
To use this for troubleshooting DataContext issues simply set the DataContext.Log = New DebuggerWriter such as this:
Dim tran As New Transactions.TransactionScope
Using tran
db.Log = New DebuggerWriter
db.SubmitChanges(ConflictMode.ContinueOnConflict)
'rollback by disposing without complete
tran.Dispose()
End Using
This will submit the changes, spit out all the SQL code to the Debugger Window and roll everything back.
Copyright © 2003-2004 H. Steele Price, IV -
All opinions are my own, not necessarily those of my employer, your mother, or any government agency.