Introduction

I’m trying out Simple.Data and one of the things you always want to see from an ORM is what SQL it generates for you. Not that we don’t trust the author but still.

In the case of Nhibernate is simple, you just use NHProf. For Simple.Data there is no such thing. But there is an easy way to see it by intercepting the Trace information Mark was so gentle to provide for us.

Tracing

First of all, this is the test code I used.

Option Strict Off

Imports System.Data.SqlServerCe
Imports System.IO

Module Module1

    Sub Main()
        CreateDatabase()
        CreateTablePerson()
        Dim db = Simple.Data.Database.OpenConnection("DataSource=""test.sdf""; Password=""mypassword""")
        db.Person.Insert(New With {.LastName = "lastname1", .FirstName = "firstname1"})
        db.Person.Insert(New With {.LastName = "lastname1", .FirstName = "firstname2"})
        Dim result = db.Persons.FindAllByLastName("lastname1")
        For Each person In result
            Console.WriteLine(person.LastName & " " & person.FirstName)
        Next
        Dim count = db.Persons.FindAllByLastName("lastname1").Count
        Console.WriteLine("Count: " & count)
        Console.ReadLine()
    End Sub

    Private Function CreateDatabase() As SqlCeEngine
        If File.Exists("test.sdf") Then File.Delete("test.sdf")
        Dim connectionString = "DataSource=""test.sdf""; Password=""mypassword"""
        Dim en = New SqlCeEngine(connectionString)
        en.CreateDatabase()
        Return en
    End Function

    Private Sub CreateTablePerson()
        Using cn = New SqlCeConnection("DataSource=""test.sdf""; Password=""mypassword""")
            If cn.State = ConnectionState.Closed Then
                cn.Open()
            End If
            Dim sql = "create table Person (LastName nvarchar (40) not null, FirstName nvarchar (40))"
            Using cmd = New SqlCeCommand(sql, cn)
                cmd.ExecuteNonQuery()
            End Using
        End Using
    End Sub
End Module

So where does that trace information end up.

Normally it should end up in the Output window under Debug.

But this depends on a little setting in Tools->Options->Debugging

If this options is checked than the output appears in the immediate window.

But you can also add tracelisteners to output the traceoutput to somewhere else.

This will just add them to your console output.

vbnet Trace.Listeners.Add(New TextWriterTraceListener(Console.Out)) Which will output this.

You can read all about Tracelisteners on MSDN.