在搜索使用LINQ TO SQL 添加数据后获得自增长ID的方法时,发现C#可以使用DebuggerWritter把使用Linq to SQL执行的SQL语句显示到即时窗口,于是在网上搜索到在VB.NET下实现的方法,共享给大家:

1、首先在项目内添加新类,命名为:DebuggerWritter.vb

2、输入代码后保存:

  1. Imports System.Diagnostics
  2. Imports System.Globalization
  3. Imports System.IO
  4. Imports System.Text
  5.  
  6. ''' <summary>
  7. ''' Implements a <see cref="TextWriter"/> for writing information to the debugger log.
  8. ''' </summary>
  9. ''' <seealso cref="Debugger.Log"/>
  10. Public Class DebuggerWriter
  11. Inherits TextWriter
  12. Private _isOpen As Boolean
  13. Private Shared _encoding As UnicodeEncoding
  14. Private ReadOnly _level As Integer
  15. Private ReadOnly _category As String
  16.  
  17. ''' <summary>
  18. ''' Initializes a new instance of the <see cref="DebuggerWriter"/> class.
  19. ''' </summary>
  20. Public Sub New()
  21. Me.New(, Debugger.DefaultCategory)
  22. End Sub
  23.  
  24. ''' <summary>
  25. ''' Initializes a new instance of the <see cref="DebuggerWriter"/> class with the specified level and category.
  26. ''' </summary>
  27. ''' <param name="level">A description of the importance of the messages.</param>
  28. ''' <param name="category">The category of the messages.</param>
  29. Public Sub New(ByVal level As Integer, ByVal category As String)
  30. Me.New(level, category, CultureInfo.CurrentCulture)
  31. End Sub
  32.  
  33. ''' <summary>
  34. ''' Initializes a new instance of the <see cref="DebuggerWriter"/> class with the specified level, category and format provider.
  35. ''' </summary>
  36. ''' <param name="level">A description of the importance of the messages.</param>
  37. ''' <param name="category">The category of the messages.</param>
  38. ''' <param name="formatProvider">An <see cref="IFormatProvider"/> object that controls formatting.</param>
  39. Public Sub New(ByVal level As Integer, ByVal category As String, ByVal formatProvider As IFormatProvider)
  40. MyBase.New(formatProvider)
  41. Me._level = level
  42. Me._category = category
  43. Me._isOpen = True
  44. End Sub
  45.  
  46. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  47. _isOpen = False
  48. MyBase.Dispose(disposing)
  49. End Sub
  50.  
  51. Public Overloads Overrides Sub Write(ByVal value As Char)
  52. If Not _isOpen Then
  53. Throw New ObjectDisposedException(Nothing)
  54. End If
  55. Debugger.Log(level, category, value.ToString())
  56. End Sub
  57.  
  58. Public Overloads Overrides Sub Write(ByVal value As String)
  59. If Not _isOpen Then
  60. Throw New ObjectDisposedException(Nothing)
  61. End If
  62. If value <> Nothing Then
  63. Debugger.Log(level, category, value)
  64. End If
  65. End Sub
  66.  
  67. Public Overloads Overrides Sub Write(ByVal buffer As Char(), ByVal index As Integer, ByVal count As Integer)
  68. If Not _isOpen Then
  69. Throw New ObjectDisposedException(Nothing)
  70. End If
  71. If buffer = Nothing OrElse index < OrElse count < OrElse buffer.Length - index < count Then
  72. ' delegate throw exception to base class
  73. MyBase.Write(buffer, index, count)
  74. End If
  75. Debugger.Log(level, category, New String(buffer, index, count))
  76. End Sub
  77.  
  78. Public Overloads Overrides ReadOnly Property Encoding() As Encoding
  79. Get
  80. If _encoding Is Nothing Then
  81. _encoding = New UnicodeEncoding(False, False)
  82. End If
  83. Return _encoding
  84. End Get
  85. End Property
  86.  
  87. Public ReadOnly Property Level() As Integer
  88. Get
  89. Return Level
  90. End Get
  91. End Property
  92.  
  93. Public ReadOnly Property Category() As String
  94. Get
  95. Return _category
  96. End Get
  97. End Property
  98. End Class

3、在项目中添加对System.Transactions的引用:

4、在Linq TO SQL执行处添加代码:

  1. Dim tran As New Transactions.TransactionScope
  2. Using tran
  3. db.Log = New DebuggerWriter
  4. db.SubmitChanges()
  5. tran.Dispose()
  6. End Using

5、在VS.NET中打开“即时窗口”

6、运行程序,即可在“即时窗口”中看到转换后的SQL代码:

最后注意:这种方法运行程序后执行的操作只反映在“即时窗口”中,并不会真正的更改数据库内容,所以在调试完成后请将调试代码删除。

[转载代码]VB.NET 中查询 Linq to SQL 执行时的SQL语句的更多相关文章

  1. VB.NET中使用Linq TO SQL添加数据后获得自增长列ID

    VB.NET中使用Linq TO SQL添加数据后获得自增长列ID: Dim tempOrdre As New Order With { .CustomerID = cmbCustomerName.S ...

  2. 从数据库中查询所有表及所有字段的SQL语句

    从数据库中查询所有表及所有字段的SQL语句 由于一个小项目的需要,近日完成一个从数据库中查询所有表及所有字段的方法,其实用两条SQL语句就可以完成. Sql Server版:列出当前DB中所有表:se ...

  3. [转载]在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 ...

  4. mysql 中查询一个字段是否为null的sql

    查询mysql数据库表中字段为null的记录: select * 表名 where 字段名 is null 查询mysql数据库表中字段不为null的记录: select * 表名 where 字段名 ...

  5. SQL Server 从数据库中查询去年的今天的数据的sql语句

    因为最近的项目的一个小功能需要实现当前数据和历史的今天做一个对比.在网上也查了很久,很多都是实现一个月内的,一年内的所有数据,昨晚突然就找到了下面的实现方法,在SQL Server2008中试了一下, ...

  6. 表中查询重复的数据,如何通过sql语句查询?

    1.最直观的思路:要知道所有名字有重复人资料,首先必须知道哪个名字重复了:select name from emp group by name having count(*)>1所有名字重复人的 ...

  7. Moq中判断方法是否被执行时,参数中有列表的情况

    如果参数中有列表,列表项为引用类型时,则会判断列表项是否为同一引用 列表本身不判断

  8. 在VB中使用Linq To SQLite注意事项

    昨天使Linq To SQLite 支持VB,今天在VB中写了几条Linq语句,发现了几个问题: 1.在Linq To SQLite中的Linq语句查询后并不是得到的匿名数据类,而是将Linq转换为S ...

  9. Unity3D C#中使用LINQ查询(与 SQL的区别)

    学过SQL的一看就懂 LINQ代码很直观 但是,LINQ却又跟SQL完全不同 首先来看一下调用LINQ的代码 int[] badgers = {36,5,91,3,41,69,8}; var skun ...

随机推荐

  1. Hadoop configration类分析

    configration这个类是分析hadoop源代码一个很好地入口. 先从需求说起.对于一个大型的文件系统,基于配置文件可以增强灵活性.congfigration类就是为了管理配置文件的. 配置文件 ...

  2. .Net中的各种序列化

    我们知道将对象的状态保持在存储媒体中,以便可以在以后重新创建精确的副本这正是数据持久化所要做的.而且,不同应用程序之间的通讯需要相互传输数据.那么序列化和反序列化正是为此而生. 序列化和反序列化 所谓 ...

  3. SPFile的使用

    转:http://blog.csdn.net/pclzr/article/details/7591741 SPFile对应于SharePoint对象模型中的文件,它的使用方法与SPFolder类大致相 ...

  4. hihoCoder 1385 A Simple Job

    #1385 : A Simple Job 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Institute of Computational Linguistics (I ...

  5. java web 学习十六(JSP指令)

    一.JSP指令简介 JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: pa ...

  6. spring中的BeanFactory与ApplicationContext的作用和区别?

    BeanFactory类关系继承图 1. BeanFactory类结构体系: BeanFactory接口及其子类定义了Spring IoC容器体系结构,由于BeanFactory体系非常的庞大和复杂, ...

  7. Mysql 多表联合查询效率分析及优化

    1. 多表连接类型 1. 笛卡尔积(交叉连接) 在MySQL中可以为CROSS JOIN或者省略CROSS即JOIN,或者使用','  如: SELECT * FROM table1 CROSS JO ...

  8. E asy Boo t 6.51 启动易 制作启动光盘的软件(附注册码)

    内建ISO文件生成器,可直接生成可启动ISO文件,并支持N合1优化. -------中文版注册码------- 用户名:中华人民共和国 注册码:2898-5448-5603-BB2D -------英 ...

  9. codeforces 696C PLEASE 概率dp+公式递推+费马小定理

    题意:有3个杯子,排放一行,刚开始钥匙在中间的杯子,每次操作,将左右两边任意一个杯子进行交换,问n次操作后钥匙在中间杯子的概率 分析:考虑动态规划做法,dp[i]代表i次操作后的,钥匙在中间的概率,由 ...

  10. 获取json中字段,判断是否有想要的key

    if(json.containsKey("key")){ String refundSid = json.getString("key"); } 如果也要判断v ...