SqlClient使用存储过程并获取输出参数的指

public static int AddUser(Entity.UserInfo user)
        {
     int id =
0;
            //使用存储过程实现添加数据
           
//proc_AddUser为存储过程名称
            using (SqlCommand command = new
SqlCommand("proc_AddUser", DBService.Conn))
            {
               
//指定command对象的执行方式
                command.CommandType =
CommandType.StoredProcedure;
               
//指定存储过程的参数并赋值
               
command.Parameters.Add("@uName",SqlDbType.NVarChar,20).Value = user.Name; 

                command.Parameters.Add("@uAge", SqlDbType.Int).Value =
user.Age;
                command.Parameters.Add("@uPass",
SqlDbType.NVarChar, 200).Value = user.Password;
               
//设置输出参数
                command.Parameters.Add("@uId",
SqlDbType.Int).Direction                           

                 =ParameterDirection.Output;   //@uId,@uName等参数必须与数据库存储过程中的参数一致
  //执行
  command.ExecuteNonQuery();
  //获取输出参数的值
               
id = Convert.ToInt32(command.Parameters["@uId"].Value);
           
}
  return id;
        }

原文链接:http://renhappy20066.blog.163.com/blog/static/1120807862010220103254236/

原文地址:https://www.cnblogs.com/bmate/p/2342406.html