.net 获取 存储过程的输出参数

存储过程: 做的就是 返回最后一次插入的标识列 id值

 

create proc pa2

@id int output
as
insert into a(name) values('5')
set @id=@@identity

表: id 是标识列 ,

 

image

 

后台代码: 

 string cons = ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString();

        using (SqlConnection con=new SqlConnection(cons))
        {
            if (con.State==ConnectionState.Closed)
            {
                con.Open();
                
            }

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "pa2";

            cmd.Parameters.Add(new SqlParameter("@id",DbType.Int32));

            cmd.Parameters["@id"].Direction = ParameterDirection.Output;

            cmd.ExecuteNonQuery();
            con.Close();
            Response.Write(cmd.Parameters["@id"].Value.ToString());
            

        }

 

 

效果:

image

posted @ 2012-09-11 12:30  左正  阅读(299)  评论(0编辑  收藏  举报