您的位置:首页 > 数据库

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

2012-02-08 11:19 232 查看
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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐