如何在ServiceStack.OrmLite中使用参数执行SQL语句?

How do I execute a SQL statement with parameters in ServiceStack.OrmLite?
2021-02-23
  •  译文(汉语)
  •  原文(英语)

我想在ServiceStack Ormlite中使用参数执行SQL语句

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

我想要最好的方法.

解决过程1

如果需要POCO结果,可以使用:

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

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

解决过程2

您可以使用SqlScalar<T>其中Tint.然后,只需使用您的参数传递一个匿名对象即可.

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

选择一个List<T>POCO类型而不是一个,int您可以使用:

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

您可以在官方文档示例中阅读更多内容.

希望这可以帮助.

I want to execute SQL statement with paraemeters in ServiceStack ormlite

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

I want the best way.

Solutions1

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

Solutions2

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

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入