C#存储过程Output返回参数 方法调用类

存储部分:

create proc proc_GetUserID 

@UserName nvarchar(50),@ID int output 

as

begin

    set @ID = (select ID from UserAccount where UserName = @UserName)

end

  C代码:

private void GetUserID(string userName)

        {

            SqlParameter[] paras = new SqlParameter[2];

            paras[0] = new SqlParameter("@UserName", userName);

            paras[1] = new SqlParameter("@ID",SqlDbType.Int);

            paras[1].Direction = ParameterDirection.Output;

            object o = ExcuteNonQuery_Proc_Output("proc_GetUserID", paras, "@ID");

         

        }
 public static object ExcuteNonQuery_Proc_Output(string procName, SqlParameter[] parameters,string outName)

        {

            SqlConnection conn = GetConnection();

            SqlCommand cmd = new SqlCommand();

            cmd.Connection = conn;

            cmd.CommandType = CommandType.StoredProcedure;

            cmd.CommandText = procName;

            for (int i = 0; i < parameters.Length; i++)

            {

                cmd.Parameters.Add(parameters[i]);

            }

            conn.Open();

            int n = cmd.ExecuteNonQuery();

            object o = cmd.Parameters[outName].Value;

            conn.Close();

            return o;

        }

猜你喜欢

转载自www.cnblogs.com/Striveyoungfellow/p/12508696.html