-
[转载代码]VB.NET 中查询 Linq to SQL 执行时的SQL语句
在搜索使用LINQ TO SQL 添加数据后获得自增长ID的方法时,发现C#可以使用DebuggerWritter把使用Linq to SQL执行的SQL语句显示到即时窗口,于是在网上搜索到在VB.NET下实现的方法,共享给大家:
1、首先在项目内添加新类,命名为:DebuggerWritter.vb
2、输入代码后保存:
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
3、在项目中添加对System.Transactions的引用:
4、在Linq TO SQL执行处添加代码:
Dim tran As New Transactions.TransactionScope Using tran db.Log = New DebuggerWriter db.SubmitChanges() tran.Dispose() End Using
5、在VS.NET中打开“即时窗口”
6、运行程序,即可在“即时窗口”中看到转换后的SQL代码:
最后注意:这种方法运行程序后执行的操作只反映在“即时窗口”中,并不会真正的更改数据库内容,所以在调试完成后请将调试代码删除。
出处:https://www.cnblogs.com/lxzhangying/p/3220373.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式