准备的工作
俩层架构的框架
1-数据访问层—DAL;
2-实体类—Models;
3-UI层—StudentManagePro;
总体架构展示:
SQL表:
UI界面展示
编写后台方法StudentService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Models;
namespace DAL
{
//添加学员对象数据访问类
public class StudentService
{
//添加学员对象
public int AddStudent(Students objStudent)
{
string sql = "insert into Students (StudentName,Age,Gender,Bitrthday,";
sql += "CardNo,ClassId,StudentIdNo,PhoneNumber,StudentAddress,StuImage) ";
sql += " Values('{0}',{1},'{2}','{3}','{4}',{5},{6},'{7}','{8}','{9}')";
sql = string.Format(sql, objStudent.StudentName, objStudent.Age, objStudent.Gender,
objStudent.Birthday.ToString("yyyy-MM-dd"), objStudent.CardNo,
objStudent.ClassId, objStudent.StudentIdNo, objStudent.PhoneNumber,
objStudent.StudentAddress, objStudent.StuImage);
try
{
return SQLHelper.Update(sql);
}
catch (Exception ex)
{
throw new Exception("保存数据出现问题" + ex.Message);
}
}
}
}
封装对象
//封装学员对象
Students objStudent = new Students()
{
StudentName = this.txtStuName.Text.Trim(),
Gender = this.rdoMale.Checked ? "男" : "女",
Birthday = Convert.ToDateTime(this.txtBirthday.Text),
Age = DateTime.Now.Year - Convert.ToDateTime(this.txtBirthday.Text).Year,
ClassId = Convert.ToInt32(this.cboClssName.SelectedValue),
StudentIdNo = Convert.ToDecimal(this.txtStuIdNo.Text.Trim()),
CardNo = this.txtCardNo.Text.Trim(),
PhoneNumber = this.txtPhoneNumber.Text.Trim(),
StudentAddress = this.txtStudAddress.Text.Trim(),
StuImage = this.pbStu.Image == null?"":new Common.SerializeObjectToString()
.SerializeObject(this.pbStu.Image)
};
提交对象
创建后台访问对象:
SQL表格内容: