存储过程: 做的就是 返回最后一次插入的标识列 id值
create proc pa2 @id int output as insert into a(name) values('5') set @id=@@identity
表: id 是标识列 ,
后台代码:
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()); }
效果:
分类:
SQL
本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/archive/2012/09/11/2680001.html
,如需转载请自行联系原作者

