How do I execute a SQL statement with parameters in ServiceStack.OrmLite?

对着背影说爱祢 提交于 2019-12-23 06:41:39

问题


I want to execute SQL statement with paraemeters in ServiceStack ormlite

String.Format("SELECT OBJECT_ID(@name)", name);

I want the best way.


回答1:


If you need the POCO result you can use:

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

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




回答2:


You can use SqlScalar<T> where T is int. Then simply pass an anonymous object with your parameter.

int result = db.SqlScalar<int>("SELECT OBJECT_ID(@name)", new { name = "SomeName" });

to select a List<T> of POCO type rather than an int you can use:

var results = db.SqlList<User>("SELECT * FROM Users WHERE Name = @name", new { name = "SomeName" });

You can read more here in the official documentation examples.

Hope this helps.



来源:https://stackoverflow.com/questions/23733432/how-do-i-execute-a-sql-statement-with-parameters-in-servicestack-ormlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!