学习了如何编写通用数据访问类SQLHelper,在此基础上还可以继续优化程序,基于对象职责明确原则,将程序封装成学生数据信息访问类、班级信息访问类、成绩信息访问类等。
1 /// <summary> 2 /// 学员信息数据访问类 3 /// </summary> 4 class StudentService 5 { 6 public int AddStudent(string studentName, string gender, DateTime birthday, 7 string stuIdNo, int age, string phoneNumber, string stuAddress, int classId) 8 { 9 string sql = "insert into Students(StudentName,Gender,Birthday,studentIdNo,Age,PhoneNumber,studentAddress, classId)"; 10 sql += "values(‘{0}‘,‘{1}‘,‘{2}‘,{3},{4},‘{5}‘,‘{6}‘,{7})"; 11 sql = string.Format(sql, studentName, gender, birthday, stuIdNo, age, phoneNumber, stuAddress, classId); 12 return SQLHelper.Update(sql); 13 }14 }
1 static void Main(string[] args) 2 { 3 //获取用户输入 4 Console.WriteLine("请输入学员姓名:"); 5 string stuName = Console.ReadLine(); 6 Console.WriteLine("请输入学员性别:"); 7 string gender = Console.ReadLine(); 8 Console.WriteLine("请输入学员出生日期:"); 9 DateTime birthday = Convert.ToDateTime(Console.ReadLine()); 10 Console.WriteLine("请输入学员身份证号:"); 11 string stuIdNo = Console.ReadLine(); 12 Console.WriteLine("请输入学员年龄:"); 13 int age = Convert.ToInt16(Console.ReadLine()); 14 Console.WriteLine("请输入学员电话:"); 15 string phoneNumber = Console.ReadLine(); 16 Console.WriteLine("请输入学员身份地址:"); 17 string stuAddress = Console.ReadLine(); 18 Console.WriteLine("请输入学员班级编号:"); 19 int classId = Convert.ToInt16(Console.ReadLine()); 20 21 //调用数据访问方法 22 StudentService objstuService = new StudentService(); 23 int result = objstuService.AddStudent(stuName, gender, birthday, stuIdNo, age, phoneNumber, stuAddress, classId); 24 //展示结果 25 Console.WriteLine(result); 26 Console.ReadLine(); 27 }
但是以上代码存在缺点:方法参数多
1.定义和使用不方便,容易把参数写错
2.当对象是属性变化时,方法的参数必须改变
问题解学办法:使用实体类作为方法参数,稳定对外接口
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ASP.NETDemo 8 { 9 /// <summary> 10 /// 学生实体类 11 /// </summary> 12 class Student 13 { 14 public int StudentId { get; set; } 15 public string StudentName { get; set; } 16 public string Gender { get; set; } 17 public DateTime Birthday { get; set; } 18 public decimal StudentIdNo { get; set; } 19 public int Age { get; set; } 20 public string PhoneNumber { get; set; } 21 public string StudentAddress { get; set; } 22 public int ClassId { get; set; } 23 } 24 }
优化后
1 public int AddStudent(Student objStudent) 2 { 3 string sql = "insert into Students(StudentName,Gender,Birthday,studentIdNo,Age,PhoneNumber,studentAddress, classId)"; 4 sql += "values(‘{0}‘,‘{1}‘,‘{2}‘,{3},{4},‘{5}‘,‘{6}‘,{7})"; 5 sql = string.Format(sql,objStudent.StudentName,objStudent.Gender,objStudent.Birthday,objStudent.StudentIdNo,objStudent.Age,objStudent.PhoneNumber,objStudent.StudentAddress, objStudent.ClassId); 6 return SQLHelper.Update(sql); 7 }
1 static void Main(string[] args) 2 { 3 //创建学员对象 4 Student objStu = new Student(); 5 6 //获取用户输入,封装学员对象 7 Console.WriteLine("请输入学员姓名:"); 8 objStu.StudentName = Console.ReadLine(); 9 Console.WriteLine("请输入学员性别:"); 10 objStu.Gender = Console.ReadLine(); 11 Console.WriteLine("请输入学员出生日期:"); 12 objStu.Birthday = Convert.ToDateTime(Console.ReadLine()); 13 Console.WriteLine("请输入学员身份证号:"); 14 objStu.StudentIdNo =Convert.ToDecimal( Console.ReadLine()); 15 Console.WriteLine("请输入学员年龄:"); 16 objStu.Age = Convert.ToInt16(Console.ReadLine()); 17 Console.WriteLine("请输入学员电话:"); 18 objStu.PhoneNumber = Console.ReadLine(); 19 Console.WriteLine("请输入学员身份地址:"); 20 objStu.StudentAddress = Console.ReadLine(); 21 Console.WriteLine("请输入学员班级编号:"); 22 objStu.ClassId = Convert.ToInt16(Console.ReadLine()); 23 24 //调用数据访问方法 25 StudentService objstuService = new StudentService(); 26 int result = objstuService.AddStudent(objStu); 27 //展示结果 28 Console.WriteLine(result); 29 Console.ReadLine(); 30 }
时间: 2024-11-05 23:34:51