Search code examples
c#mysqlormlite-servicestack

ServiceStack.OrmLite: Where is the method to write custom SQL and get result set back?


I have been reading on https://github.com/ServiceStack/ServiceStack.OrmLite to find methods so I can execute normal SQL (string commands), and get a result set back, but I don't think there is any.

I am testing OrmLite v 4.0.17.0.

On the page above, there is a method SqlList mentioned, but I have no such method available:

enter image description here

I have an ExecuteSql, but I get no result set back, just an int:

enter image description here

I have two questions:

  1. Is there a way to execute custom queries, a method that takes in a string as parameter, and where I then get back a result set, rows or objects or something?
  2. And while I am at it, how do I create a "LIMIT", for example "SELECT * FROM a LIMIT 10"?

Here is the version info:

enter image description here


Solution

  • Yeah I recently noticed that db.SqlList got lost in the OrmLite v4 refactor so I've restored it back in this commit. Which will be available in the next release of v4, before the end of the month.

    You can still use db.Select for raw SELECT queries, e.g:

    var results = db.Select<Poco>("SELECT * FROM a LIMIT 10");
    

    It's only an issue when it's not a SELECT statement because we'd assume it's short-hand like:

    var results = db.Select<Poco>("Foo = 'bar'");
    

    And automatically add the rest of the SQL for you, but this is a problem when you're not issuing a SELECT statement, e.g. calling a stored procedure, which is what db.SqlList is for since the raw SQL remains untouched.

    Support for LIMIT in Typed Expression API

    Another way to query with a limit is to use the typed expression API, e.g:

    var results = db.Select<Poco>(q => q.Limit(10));