1.在数据库中创建删除的存储过程
create proc proc_DelStudent
@studentId int
as
begin
delete from tb_Students where studentId=@studentId
return 100
end
2.在类dal_sql中创建 执行增删除存储过程的方法
public void ExcuteProc(string 存储过程名,SqlParameter[] pars//存储过程中用到的参数)
{
conn = GetConn();获取数据库连接
SqlCommand cmd = new SqlCommand(存储过程名, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(pars);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
#endregion
调用存储过程
public int DelStudent(int id)
{
SqlParameter[] pars = new SqlParameter[2];
pars[0] = new SqlParameter("@studentId", id);
pars[1] = new SqlParameter("returnValue", SqlDbType.Int);
pars[1].Direction = ParameterDirection.ReturnValue;
dal_sql.ExcuteProc("proc_DelStudent", pars);
return (int)pars[1].Value;
}
取消
评论