[转载代码]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(, 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 < OrElse count < 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代码:

最后注意:这种方法运行程序后执行的操作只反映在“即时窗口”中,并不会真正的更改数据库内容,所以在调试完成后请将调试代码删除。
[转载代码]VB.NET 中查询 Linq to SQL 执行时的SQL语句的更多相关文章
- VB.NET中使用Linq TO SQL添加数据后获得自增长列ID
VB.NET中使用Linq TO SQL添加数据后获得自增长列ID: Dim tempOrdre As New Order With { .CustomerID = cmbCustomerName.S ...
- 从数据库中查询所有表及所有字段的SQL语句
从数据库中查询所有表及所有字段的SQL语句 由于一个小项目的需要,近日完成一个从数据库中查询所有表及所有字段的方法,其实用两条SQL语句就可以完成. Sql Server版:列出当前DB中所有表:se ...
- [转载]在VB.Net中获取COM对象的特定实例(Getting a specific instance of COM object in VB.Net)
转载:http://www.it1352.com/534235.html 问题: I am writing a Windows Form application in .Net to list all ...
- mysql 中查询一个字段是否为null的sql
查询mysql数据库表中字段为null的记录: select * 表名 where 字段名 is null 查询mysql数据库表中字段不为null的记录: select * 表名 where 字段名 ...
- SQL Server 从数据库中查询去年的今天的数据的sql语句
因为最近的项目的一个小功能需要实现当前数据和历史的今天做一个对比.在网上也查了很久,很多都是实现一个月内的,一年内的所有数据,昨晚突然就找到了下面的实现方法,在SQL Server2008中试了一下, ...
- 表中查询重复的数据,如何通过sql语句查询?
1.最直观的思路:要知道所有名字有重复人资料,首先必须知道哪个名字重复了:select name from emp group by name having count(*)>1所有名字重复人的 ...
- Moq中判断方法是否被执行时,参数中有列表的情况
如果参数中有列表,列表项为引用类型时,则会判断列表项是否为同一引用 列表本身不判断
- 在VB中使用Linq To SQLite注意事项
昨天使Linq To SQLite 支持VB,今天在VB中写了几条Linq语句,发现了几个问题: 1.在Linq To SQLite中的Linq语句查询后并不是得到的匿名数据类,而是将Linq转换为S ...
- Unity3D C#中使用LINQ查询(与 SQL的区别)
学过SQL的一看就懂 LINQ代码很直观 但是,LINQ却又跟SQL完全不同 首先来看一下调用LINQ的代码 int[] badgers = {36,5,91,3,41,69,8}; var skun ...
随机推荐
- LA 2218 (半平面交) Triathlon
题意: 有n个选手,铁人三项有连续的三段,对于每段场地选手i分别以vi, ui 和 wi匀速通过. 对于每个选手,问能否通过调整每种赛道的长度使得他成为冠军(不能并列). 分析: 粗一看,这不像一道计 ...
- BZOJ_1628_[Usaco2007_Demo]_City_skyline_(单调栈)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1628 给出\(n\)个距形的影子,问最少是多少个建筑的?(建筑的影子可以重叠). 分析 用单调 ...
- sqlserver错误"试图扩大物理文件时,MODIFY FILE 遇到操作系统错误 112(磁盘空间不足。)。"处理
正常还原的时候报错: Microsoft SQL-DMO (ODBC SQLState: 42000)---------------------------试图扩大物理文件时,MODIFY FILE ...
- android通过httpClient请求获取JSON数据并且解析
使用.net创建一个ashx文件,并response.write json格式 public void ProcessRequest(HttpContext context) { context.R ...
- [反汇编练习]160个CrackMe之001
[反汇编练习] 160个CrackMe之001. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- django - 修改 request.POST的值
# querydict改为mutable data = data.copy() data.update({'key_list': DATA_UPLOAD_PARAMETER}) 默认的request. ...
- Java [Leetcode 172]Factorial Trailing Zeroes
题目描述: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be ...
- SpringMVC——实现拦截器
1. SpringMVC拦截器的概念与Struts2相同 2. 实现拦截器 (1) 项目结构 (2) 实现HandlerInterceptor接口 package com.zhengbin.contr ...
- iwpriv工具通过ioctl动态获取相应无线网卡驱动的private_args所有扩展参数
iwpriv工具通过ioctl动态获取相应无线网卡驱动的private_args所有扩展参数 iwpriv是处理下面的wlan_private_args的所有扩展命令,iwpriv的实现上,是这样的, ...
- IOS 多级列表展开控件
项目中实现了一个可以多级展开的列表控件.每次展开都是互斥的,就是说,展开一个cell 就会关闭其他展开的层. 可以呈现的效果如下图.第一个图片是应用中实现的效果.第二个是Demo中的效果.如果有新的需 ...