C#存储过程:插入一条数据,返回自增列ID值
2012/11
30
16:11
表UserAccount结构:
ID int 自增主键
UserName nvarchar(50)
Password nvarchar(50)
Used bit 默认值:1
create proc proc_AddUserAccount @UserName nvarchar(50),@Password nvarchar(50) as begin insert into UserAccount (UserName,Password) values (@UserName,@Password) return @@identity end
protected void ButtonRegist_Click(object sender, EventArgs e) { string userName = this.TextBoxUserName.Text.Trim(); string password = this.TextBoxPass.Text; SqlParameter[] paras = new SqlParameter[3]; paras[0] = new SqlParameter("@UserName",userName); paras[1] = new SqlParameter("@Password", password); paras[2] = new SqlParameter("@RETURN_VALUE", ""); paras[2].Direction = ParameterDirection.ReturnValue; object o = DataAccess.ExcuteNonQuery_Proc_Return("proc_AddUserAccount", paras); Response.Write(o.ToString()); }
public class DataAccess { private static string connectionString = ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString; private static SqlConnection GetConnection() { SqlConnection conn = new SqlConnection(connectionString); return conn; } public static object ExcuteNonQuery_Proc_Return(string procName, SqlParameter[] parameters) { 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["@RETURN_VALUE"].Value; conn.Close(); return o; } }
CopyRights: The Post by BY-NC-SA For Authorization,Original If Not Noted,Reprint Please Indicate From NewMiniSoft
Post Link: C#存储过程:插入一条数据,返回自增列ID值
Post Link: C#存储过程:插入一条数据,返回自增列ID值
发表回复