vamosCheng

导航

output和returnvalue的作用

   贴两段代码。

1>

  public int ExecuteNonQuery(string pro, MobileOrder or)
        {
            SqlParameter[] sp = {
            new SqlParameter("@customer",SqlDbType.NVarChar,10),
            new SqlParameter("@IDCardNo",SqlDbType.NVarChar,18),
            new SqlParameter("@SaleID",SqlDbType.Int),
            new SqlParameter("@MobileModelID",SqlDbType.Int),
            new SqlParameter("@Amount",SqlDbType.Int),
            new SqlParameter("@PayFee",SqlDbType.Float)
                               };
            sp[0].Direction = ParameterDirection.Output;
            sp[1].Value = or.IDCardNo;
            sp[2].Value = or.SaleID;
            sp[3].Value = or.MobileModelID;
            sp[4].Value = or.Amount;
            sp[5].Direction = ParameterDirection.Output;

            db.ExecuteNonQuery(sql, ref sp);
            return sp[0].value;

        }

 

2>

 public int ExecuteNonQuery(string sql, ref SqlParameter[] sp)
        {
            using (SqlConnection conn = new SqlConnection(sqlCon))
            {
                try
                {
                    SqlCommand comm = new SqlCommand();
                    comm.CommandType = CommandType.StoredProcedure;
                    comm.CommandText = sql;
                    comm.Connection = conn;
                    if (sp != null)
                    {
                        comm.Parameters.AddRange(sp);
                    }
                    conn.Open();
                    return comm.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    return 0;
                }
                finally
                {
                    conn.Close();
                }
            }

        }

    第一段代码中声明一个sqlparameter的数组,该数组中有两个元素是output类型。第一段代码的最后调用了第二段代码所代表的方法。output的作用就是经过调用后,编译器自动将执行数据库操作后的相应的数值赋给性质为output的元素,因此在第一段代码中调用sp[0]和sp[5],相应的value值发生变化,而sp[1]、sp[2]、sp[3]、sp[4]的数值没有发生变化。

    学习笔记。如有错误,欢迎指正。

posted on 2013-08-07 18:09  vamosCheng  阅读(267)  评论(0编辑  收藏  举报