public class GradeDal
{
public int AddGrade(Grade Grade)
{
int Result = 0;
using (SqlConnection scon = new SqlConnection("Server=.;uid=sa;pwd=1234;database=StudyQutionDB"))
{
SqlCommand scom = new SqlCommand("AddGrade", scon);
scom.CommandType = System.Data.CommandType.StoredProcedure; // 指定命令类型为存储过程
scom.Parameters.Add(new SqlParameter("@Name", Grade.Name)); // Parameters 参数集合
scom.Parameters.Add(new SqlParameter("@Remark", Grade.Remark));
scon.Open();
Result = scom.ExecuteNonQuery();
scon.Close();
}
return Result;
}
public int UpdateGrade(Grade Grade)
{
int Result = 0;
using (SqlConnection scon = new SqlConnection("Server=.;uid=sa;pwd=1234;database=StudyQutionDB"))
{
SqlCommand scom = new SqlCommand("UpdateGrade", scon);
scom.CommandType = System.Data.CommandType.StoredProcedure; // 指定命令类型为存储过程
scom.Parameters.Add(new SqlParameter("@Name", Grade.Name)); // Parameters 参数集合
scom.Parameters.Add(new SqlParameter("@Remark", Grade.Remark));
scom.Parameters.Add(new SqlParameter("@Id", Grade.Id));
scon.Open();
Result = scom.ExecuteNonQuery();
scon.Close();
}
return Result;
}
public int DelGrade(int Id)
{
int Result = 0;
using (SqlConnection scon = new SqlConnection("Server=.;uid=sa;pwd=1234;database=StudyQutionDB"))
{
SqlCommand scom = new SqlCommand("DelGrade", scon);
scom.CommandType = System.Data.CommandType.StoredProcedure; // 指定命令类型为存储过程
scom.Parameters.Add(new SqlParameter("@Id", Id));// Parameters 参数集合
scon.Open();
Result = scom.ExecuteNonQuery();
scon.Close();
}
return Result;
}
public List<Grade> GetAll()
{
List<Grade> list = new List<Grade>();
using (SqlConnection scon = new SqlConnection("Server=.;uid=sa;pwd=1234;database=StudyQutionDB"))
{
SqlCommand scom = new SqlCommand("GetGrade", scon);
scom.CommandType = System.Data.CommandType.StoredProcedure; // 指定命令类型为存储过程
scon.Open();
SqlDataReader reader = scom.ExecuteReader();
while (reader.Read())
{
Grade grade = new Grade();
grade.Id = (int)reader["Id"];
grade.Name = (string)reader["Name"];
grade.Remark = (string)reader["Remark"];
list.Add(grade);
}
reader.Close();
scon.Close();
}
return list;
}
public Grade GetGradeById(int Id)
{
Grade grade = new Grade();
using (SqlConnection scon = new SqlConnection("Server=.;uid=sa;pwd=1234;database=StudyQutionDB"))
{
SqlCommand scom = new SqlCommand("GetGradeById", scon);
scom.CommandType = System.Data.CommandType.StoredProcedure; // 指定命令类型为存储过程
scom.Parameters.Add(new SqlParameter("@Id", Id));// Parameters 参数集合
scon.Open();
SqlDataReader reader = scom.ExecuteReader();
if (reader.Read())
{
grade.Id = (int)reader["Id"];
grade.Name = (string)reader["Name"];
grade.Remark = (string)reader["Remark"];
}
reader.Close();
scon.Close();
}
return grade;
}
}
//MVC视图
public class ShitiController : Controller
{
HttpClientHelper helper = new HttpClientHelper("http://localhost:65268/");
// GET: Shiti
public ActionResult Index(int pageindex=1,int pagesize=2)
{
string sql = helper.Get($"api/Shiti/Get?pageindex={pageindex}&pagesize={pagesize}");
Shiti[] a = JsonConvert.DeserializeObject<List<Shiti>>(sql).ToArray();
int RowCount = (a == null ? 0 : a[0].pagecount);
int Count=(int)Math.Ceiling((decimal)RowCount/pagesize);
ViewBag.pagepre = pageindex <= 1 ? 1 : pageindex - 1;
ViewBag.pagenext = pageindex <= Count ? Count : pageindex + 1;
ViewBag.pagelast = Count;
return View(a.ToList());
}
public ActionResult Add()
{
return View();
}
[HttpPost]
public void Add(Shiti s)
{
s.Ri = DateTime.Now;
string sql = helper.Post("api/Shiti/Post", JsonConvert.SerializeObject(s));
int i = int.Parse(sql);
if (i > 0)
{
Response.Write("<script>alert('添加成功');location.href='/Shiti/Index'</script>");
}
else
{
Response.Write("<script>alert('添加失败');location.href='/Shiti/Add'</script>");
}
}
public void Delete(int id)
{
string sql = helper.Delete("api/Shiti/Delete/" + id);
int a = int.Parse(sql);
if (a > 0)
{
Response.Write("<script>alert('删除成功');location.href='/Shiti/Index'</script>");
}
else
{
Response.Write("<script>alert('删除失败');location.href='/Shiti/Index'</script>");
}
}
public ActionResult Upt(int id)
{
string sql = helper.Get("api/Shiti/Upt/" + id);
List<Shiti> list = JsonConvert.DeserializeObject<List<Shiti>>(sql);
return View(list.FirstOrDefault());
}
[HttpPost]
public void Upt(Shiti s)
{
string sql = helper.Post("api/Shiti/Update", JsonConvert.SerializeObject(s));
int i = int.Parse(sql);
if (i > 0)
{
Response.Write("<script>alert('修改成功');location.href='/Shiti/Index'</script>");
}
else
{
Response.Write("<script>alert('修改失败');location.href='/Shiti/Update'</script>");
}
}
MVC存储过程
最新推荐文章于 2020-02-18 10:40:43 发布