问题
Modifying an answer from this question slightly, suppose I run this code:
public int SaveOrUpdate(MyEntity entity)
{
var sql = @"MERGE INTO MyEntity
USING
(
SELECT @id as Id
@myField AS MyField
) AS entity
ON MyEntity.Id = entity.Id
WHEN MATCHED THEN
UPDATE
SET Id = @id
MyField = @myField
WHEN NOT MATCHED THEN
INSERT (Id, MyField)
VALUES (@Id, @myField);"
object[] parameters = {
new SqlParameter("@id", entity.Id),
new SqlParameter("@myField", entity.myField)
};
return context.Database.ExecuteSqlCommand(sql, parameters);
}
This will actually run and it returns an int. What does the int mean? The documentation just says
The result returned by the database after executing the command.
I did a couple tests and it looks like it's 1 if it modified a row, and 0 if nothing changed. Is the return value the number of rows modified?
回答1:
For most databases, that means the number of rows affected by the command. I theory though, god forbid that such a thing exists, the database vendor is free to return whatever and you would then need to look in the documentation for the particular database for what the number means.
来源:https://stackoverflow.com/questions/49861707/what-is-the-return-value-of-database-executesqlcommand