1. 程式人生 > >C#呼叫SQL中的儲存過程中有output引數

C#呼叫SQL中的儲存過程中有output引數

           string strsql = string.Format("Sp_AutoTenderTrialAll");
           SqlParameter[] sqlParam = new SqlParameter[]{
            new SqlParameter("@isAuto",SqlDbType.Int){Value=auto},
            new SqlParameter("@id",SqlDbType.Int){Value=id},
            new SqlParameter("@AdminId",SqlDbType.Int){Value=adminId},
            new SqlParameter("@AdminName",SqlDbType.VarChar){Value=adminName},
            new SqlParameter("@output",SqlDbType.Int,4,ParameterDirection.Output,false,0,0,string.Empty,DataRowVersion.Default,null)
           };

           return DataBase.SpExecuteNonQueryOut(strsql, CommandType.StoredProcedure, sqlParam);

       }


        /// <summary>
        /// 儲存過程
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="cmdType"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static int SpExecuteNonQueryOut(string cmdText, CommandType cmdType, SqlParameter[] values)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {

                SqlCommand cmd = new SqlCommand(cmdText, conn); //連結
                cmd.CommandType = cmdType;
                if (values != null)
                {
                    cmd.Parameters.AddRange(values);
                }
                conn.Open();
                int result = -1;
                cmd.ExecuteNonQuery();
                result = int.Parse(cmd.Parameters["@output"].Value.ToString());
               
                cmd.Dispose();
                conn.Close();
               
                return result;
                
            }
        }