zoukankan      html  css  js  c++  java
  • 关于ExecuteNonQuery执行的返回值(SQL语句、存储过程)

    因为msdn中说返回受影响的行数:

    Executes a Transact-SQL statement against the connection and returns the number of rows affected.

    但是却没看到备注里说

    For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

    对于UPDATE, INSERT,和 DELETE返回受影响的函数,但是对于所有其他类型的语句,返回值为 -1。如果发生回滚,返回值也为 -1。

    用到过ExecuteNonQuery()函数的朋友们在开发的时候肯定这么用过.

    if(cmd.ExecuteNonQuery("xxxx")>0)

    {

         //执行成功!

    }

    else

    {

         //执行失败!

    }

    通过ExecuteNonQuery()的返回值来判断操作数据库的成功与否是可以的.但是要分情况.

    1.ExecuteNonQuery() 不执行存储过程.

    此时如果对数据库执行,插入,更新,删除操作,返回的是 受影响的行数.(及一个大于等于0的整数)

    2.ExecuteNonQuery() 执行查询不返回影响的行数.

    2.ExecuteNonQuery   执行存储过程.

    [1].存储过程有返回值(传出参数)

        (1).把数据库中受影响的行数赋给返回值,这是得到的返回值是受影响的函数(大于或等于0的整数).

        (2).把某个值赋给返回值.

    [2].存储过程没有返回值

        执行成功后返回 -1.

    (没有返回值的存储过程理应 返回 受影响的行数 (执行 增删改) 但是.但我们在ado.net中执行存储过程的时候,dotnet 自动为给了存储过程一个默认值:set nocount on;

    所以给我们的感觉是执行存储过程默认返回 -1  )

    ExecuteNonQuery 返回的是最后一条SQL语句影响的行数。
    如果你想得到存储过程中的Return,那存储过程中,你必须写Return 0或Return 1。  Return 只能是int
    另外还有输出参数,可以是任意类型。
    概念别搞混了。

    假设有存储过程如下:

    SQL code
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    CREATE PROCEDURE [dbo].[sp_add]
    (
        @x int,
        @y int,
        @r int output
    )
    AS
    BEGIN
        SET NOCOUNT ON;
     
        set @r =  @x + @y;
         
        return 0;
    END



    调用方式:

    C# code
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    using (SqlConnection conn = new SqlConnection())
    {
                    conn.ConnectionString = xxxx;
                    conn.Open();
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "sp_add";
                        SqlParameter[] ps = new SqlParameter[4];
                        ps[0] = new SqlParameter("@x", 1);
                        ps[1] = new SqlParameter("@y", 2);
                        ps[2] = new SqlParameter("@r", SqlDbType.Int);
                        ps[2].Direction = ParameterDirection.Output;
                        ps[3] = new SqlParameter();
                        ps[3].SqlDbType = SqlDbType.Int;
                        ps[3].Direction = ParameterDirection.ReturnValue;
                        cmd.Parameters.AddRange(ps);
                        int r = cmd.ExecuteNonQuery();
     
                        Console.WriteLine(string.Format("@r={0},存储过程返回:{1},ExecuteNonQuery返回:{2}", ps[2].Value, ps[3].Value, r));
                    }
     }
  • 相关阅读:
    PLSQL远程访问Oracle数据库配置
    考勤系统之计算工作小时数
    C#压缩或解压(rar和zip文件)
    C#基础Queue(队列)的应用
    PetaPoco模糊查询
    牛腩购物14:商品相关表的设计 后台增加用户管理 Eval高级应用 商品类别无限分类,外键的建立,repeater嵌套repeater
    asp.net 4.0 新功能 路由
    sqlserver 2008 使用维护计划,备份数据库
    asp.net Linq和泛型,IEnumerable和IQueryable之间的区别,Lambda表达式,Linq to Sql停止开发转为 Entity Framework
    List 对象集合的操作和使用 List 集合对象 对象集合 自动属性 对象初始化 集合初始化器
  • 原文地址:https://www.cnblogs.com/net-saiya/p/9480785.html
Copyright © 2011-2022 走看看