Database.ExecuteSqlCommand的返回值是多少? [英] What is the return value of Database.ExecuteSqlCommand?

查看:245
本文介绍了Database.ExecuteSqlCommand的返回值是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改,假设我运行以下代码:

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);
}

这将实际运行并返回一个int值。 int是什么意思? 文档只是说

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.

我做了几次测试,如果修改一行,它看起来是1,如果什么都没改变,它看起来是0。返回值是修改的行数吗?

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?

推荐答案

对于大多数数据库,这意味着受命令影响的行数。不过我认为,上帝禁止这种事情存在,数据库供应商可以自由退还任何东西,然后您需要在特定数据库的文档中查找数字的含义。

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.

这篇关于Database.ExecuteSqlCommand的返回值是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆