ServiceStack OrmLite Sql Query Logging
Asked Answered
H

1

10

As per the Service Stack Ormlite documentation. I should generate the sql query in debug mode. But, I am not able to see those queries. Simple code

 private static readonly string DataDirLoc =
        Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
        "\\TargetIntegration\\Test\\Debug\\";



    private readonly string dbFileName = DataDirLoc +
                                              "Test.db3";

    [Test]
    public void Can_Generate_log() {
        //var writer = new TextWriterTraceListener(System.Console.Out);
        //Debug.Listeners.Add(writer);
        Debug.Write("this is a try");
        var dbFact = new OrmLiteConnectionFactory("Data Source={0};Version=3;".FormatParams(dbFileName), true,
                                                  SqliteOrmLiteDialectProvider.Instance);
          IDbConnection dbConnection = dbFact.OpenDbConnection();
       var dbCommand = dbConnection.CreateCommand();
        dbCommand.CreateTable<Contact>();
    }
Hinda answered 17/7, 2012 at 21:11 Comment(0)
C
15

You would need the debug build of OrmLite to see the SQL output. There are a couple of other ways you can view the last sql:

Console.WriteLine(dbCmd.GetLastSql());

You can also profile the db connection by setting a connection filter, which you can do with:

var dbFact = new OrmLiteConnectionFactory(
   "Data Source={0};Version=3;".Fmt(dbFileName), true, 
   SqliteOrmLiteDialectProvider.Instance) {
   ConnectionFilter = x => new ProfiledDbConnection(x, Profiler.Current)
};

Which if you ran this in ServiceStack will let you see the profiled timing outputs of all the SQL statements. An example of what this looks like is available here:

https://gist.github.com/1787443

Cabinda answered 17/7, 2012 at 21:41 Comment(5)
thanks Demis. But, I am using ORMlite in a windows application. I dont think I can use MVC mini profiler. May be I am wrong. Please guide.Hinda
and by default there is no extension method "GetLastSql()" on dbCommand I can use "CommandText". But it only gives the last executed statement.Hinda
Well if you hosted a ServiceStack self-hosted HttpListener service you could :) but a webserver in a WinApp seems a bit heavy for debugging. You could also create your own ProfiledDbConnection and just have it log all SQL to the console. Here's the src github.com/ServiceStack/ServiceStack/blob/master/src/…Cabinda
Update NuGet, if you don't have dbCmd.GetLastSql() but its just a wrapper around dbCmd.CommandText, so won't help if you want more than that :)Cabinda
Great Suggestions :).. I guess will create my own "ProfiledDbConnection " Thanks anyways .. great helpHinda

© 2022 - 2024 — McMap. All rights reserved.