GridView控件增删改查(查)

时间:2021-06-27 14:43:49

总共6个类~

模型层Student,Grade

数据访问层StudentServices,GradeServices

逻辑处理层StudentManager,GradeManager

UI层的绑定方法在1.7标题位置

关于1.3 DAL.StudentServices类后面Gender字段的那个详细可以搜索一下三元运算符的语法

这其中代码不考虑安全性 仅供学习参考。

没有写返回类型的方法是构造函数,分别为无参和带参

使用方法:

Student stu = new Student();
Student stu = new Student("此处参数与构造函数中的参数类型、数量相同");


1.1 Model.Student实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyWebProjectModel
{
    public class Student
    {
        /// <summary>
        /// 学号
        /// </summary>
        public string StudentNo { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string LoginPwd { get; set; }
        /// <summary>
        /// 姓名
        /// </summary>
        public string StudentName { get; set; }
        /// <summary>
        /// 性别
        /// </summary>
        public string Gender { get; set; }
        /// <summary>
        /// 年级
        /// </summary>
        public int GradeId { get; set; }
        /// <summary>
        /// 电话
        /// </summary>
        public string Phone { get; set; }
        /// <summary>
        /// 地址
        /// </summary>
        public string Address { get; set; }
        /// <summary>
        /// 日期
        /// </summary>
        public DateTime BornDate { get; set; }
        /// <summary>
        /// 邮箱
        /// </summary>
        public string Email { get; set; }
        public string GradeName { get; set; }
        /// <summary>
        /// 身份证
        /// </summary>
        public string IdentityCard { get; set; }
        public Student() { }
        /// <summary>
        /// 带参构造函数
        /// </summary>
        /// <param name="StudentNo">学号</param>
        /// <param name="LoginPwd">密码</param>
        /// <param name="StudentName">姓名</param>
        /// <param name="Gender">性别</param>
        /// <param name="GradeId">年级</param>
        /// <param name="Phone">电话</param>
        /// <param name="Address">地址</param>
        /// <param name="BornDate">日期</param>
        /// <param name="Email">邮箱</param>
        /// <param name="IdentityCard">身份证</param>
        public Student(string StudentNo, string LoginPwd, string StudentName, string Gender, int GradeId, string Phone, string Address, DateTime BornDate, string Email, string IdentityCard)
        {
            this.StudentNo = StudentNo;
            this.LoginPwd = LoginPwd;
            this.StudentName = StudentName;
            this.Gender = Gender;
            this.GradeId = GradeId;
            this.Phone = Phone;
            this.Address = Address;
            this.BornDate = BornDate;
            this.Email = Email;
            this.IdentityCard = IdentityCard;
        }
    }
}


1.2 Model.Grade实体类

<pre name="code" class="csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyWebProjectModel
{
    public class Grade
    {
        public string GradeId { get; set; }
        public string GradeName { get; set; }
    }
    public Grade(){}
    public Grade(string GradeId,string GradeName)
    {
<span style="white-space:pre">	</span>this.GradeId = GradeId;
<span style="white-space:pre">	</span>this.GradeName = GradeName;
    }
}

 

1.3 DAL.StudentServices类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyWebProjectModel;
using System.Data.SqlClient;

namespace MyWebProjectDAL
{
    public class StudentServices
    {
        public List<Student> GetStudentAll()
        {
            List<Student> list = new List<Student>();
            string sql = "select * from Student";
            using (SqlDataReader reader = DBHelper.GetExecuteReader(sql))
            {
                while (reader.Read())
                {
                    Student stu = new Student()
                    {
                        StudentNo = reader["StudentNo"].ToString(),
                        LoginPwd = reader["loginPwd"].ToString(),
                        StudentName = reader["studentName"].ToString(),
                        Gender = Convert.ToBoolean(reader["gender"]) == true ? "男" : "女",
                        GradeId = int.Parse(reader["gradeId"].ToString()),
                        GradeName = new GradeServices().GetGradeById(Convert.ToInt32(reader["gradeId"])),
                        Phone = reader["phone"].ToString(),
                        Address = reader["address"].ToString(),
                        BornDate = Convert.ToDateTime(reader["bornDate"]),
                        Email = reader["email"].ToString(),
                        IdentityCard = reader["identityCard"].ToString()
                    };
                    list.Add(stu);
                }
                return list;
            }
        }
    } 
}


1.4 DAL.GradeServices类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using MyWebProjectModel;

namespace MyWebProjectDAL
{
    public class GradeServices
    {
<span style="white-space:pre">	</span>public List<Grade> GetGradeAll()
        {
            List<Grade> list = new List<Grade>();
            string sql = "select * from Grade";
            using (SqlDataReader reader = DBHelper.GetExecuteReader(sql))
            {
                while (reader.Read())
                {
                    Grade gd = new Grade()
                    {
                        GradeId = reader["GradeId"].ToString(),
                        GradeName = reader["gradeName"].ToString()
                    };
                    list.Add(gd);
                }
            }
            return list;
        }
    }
}


1.5 BLL.StudentManager类

<pre name="code" class="csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyWebProjectDAL;
using MyWebProjectModel;


namespace MyWebProjectBLL
{
    public class StudentManager
    {


        StudentServices ss = new StudentServices();
        public List<Student> GetStudentAll()
        {
            return ss.GetStudentAll();
        }
    }
}
 


1.6 BLL.GradeManager类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyWebProjectModel;
using MyWebProjectDAL;

namespace MyWebProjectBLL
{
    public class GradeManager
    {
        GradeServices gs = new GradeServices();
        public string GetGradeById(int Id)
        {
            return gs.GetGradeById(Id);
        }
        public Grade GetGradeAllById(int Id)
        {
            return gs.GetGradeAllById(Id);
        }
        public List<Grade> GetGradeAll()
        {
            return gs.GetGradeAll();
        }
    }
}

1.7 GridView绑定

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWebProjectBLL;
using MyWebProjectModel;

namespace MyWebProject
{
    public partial class FromStudentAll : System.Web.UI.Page
    {
        StudentManager sm = new StudentManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            GetDataBind();
        }
        public void GetDataBind()
        {
            List<Student> list = sm.GetStudentAll();
            GridView1.DataSource = list;
            GridView1.DataBind();
        }
    }
}


1.8 DropDownList控件绑定

GridView控件增删改查(查)

GridView控件增删改查(查)

GridView控件增删改查(查)

此处需要注意  如果用实体类为返回类型是没作用的~

绑定方式并不是唯一 但却是比较省时间的 其他绑定方式后续案例中提到

1.3 DAL.StudentServices类