Running Raw SQL with ServiceStack.OrmLite

I am running ServiceStack.OrmLite using MS SQL Server. I would like to run raw SQL against the database, but the source documentation contains a description of how to do this only with the SELECT statement. This is not enough for me.

I cannot find a way to run anything as simple as this:

UPDATE table1 SET column1 = 'value1' WHERE column2 = value2 

Usage, for example:

 var two = db.Update(@"UPDATE table1 SET column1 = 'value1' WHERE column2 = value2"); 

Running these expressions using db.Update () or db.Update <> causes fuzzy errors, for example

Incorrect syntax next to the keyword 'UPDATE'.

I would like to use raw sql because my real UPDATE expression uses JOIN.

+4
source share
2 answers

db.Update is for updating a model or partial model, as shown in the OrmLite Documentation on Update . You can use an unencrypted type API to create an update statement, for example:

 db.Update(table: "table1", set: "column1 = {0}".Params("value1"), where: "column2 = {0}".Params("value2")); 

The Params extension method speeds up your values โ€‹โ€‹for you.

Otherwise, the way to execute arbitrary raw sql is to use db.ExecuteSql() .

+6
source

If this is a SELECT statement and you want to execute using raw sql, you can use:

 List<Person> results = db.SqlList<Person>("SELECT * FROM Person WHERE Age < @age", new { age=50}); 

Link: https://github.com/ServiceStack/ServiceStack.OrmLite#typed-sqlexpressions-with-custom-sql-apis

+1
source

Source: https://habr.com/ru/post/1490285/


All Articles