简体   繁体   English

带有 SQL 服务器的 OUTPUT 子句的 RepoDB

[英]RepoDB with SQL Server's OUTPUT clause

How can I access the contents of an OUTPUT clause with RepoDB, eg如何使用 RepoDB 访问 OUTPUT 子句的内容,例如

INSERT INTO MyTable(Name)
OUTPUT INSERTED.ID
VALUES ('TheName')

An DML with an OUTPUT clause looks to the client like a SELECT .带有OUTPUT子句的 DML 在客户端看来就像SELECT So it looks like ExecuteQuery would be the correct API.所以看起来ExecuteQuery将是正确的 API。

Use the ExecuteScalar extension method like below (only if you are inserting single row).使用如下所示的ExecuteScalar扩展方法(仅当您插入单行时)。

var result = connection.ExecuteScalar("INSERT INTO MyTable(Name) OUTPUT INSERTED.ID VALUES (@Name);", new { Name = "TheName" });

Or using the typed-result.或使用键入的结果。

var result = connection.ExecuteScalar<int>("INSERT INTO MyTable(Name) OUTPUT INSERTED.ID VALUES (@Name);", new { Name = "TheName" });

If you are inserting multiple rows, please use the ExecuteQuery method.如果要插入多行,请使用ExecuteQuery方法。 The result would be a model (of type IEnumerable<T> ).结果将是 model (类型为IEnumerable<T> )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM