MessageBoxIcon.Information);
39 return;
40 }
41 //创建学员信息对象
42 Student student = new Student();
43 // 获取要插入数据库的每个字段的值
44 student.LoginId = txtUserName.Text.Trim();
45 student.LingPwd = txtUserName.Text.Trim();
46 student.StudentName = txtName.Text.Trim();
47 student.StudentNO = txtStudentNo.Text.Trim();
48 // 班级id
49 int classID =
classManager.GetClassIDByClassName(this.cboClass.Text.Trim());
50 student.ClassID = classID;
51 // 根据选择的状态设置状态id
52 string userStateId =
radActive.Checked ? (string)radActive.Tag : (string)radInactive.Tag;
53 student.UserStateId = Convert.ToInt32(userStateId);
54 student.Sex = rdoMale.Checked ? rdoMale.Text : rdoFemale.Text;
55 // 提交学员信息
56 string message = studentManager.AddStudent(student);
57 MessageBox.Show(message, "提交提示",
MessageBoxButtons.OK, MessageBoxIcon.Information);
58
59 }
60
61 业务逻辑层
62 //对实体类对象分析,判断。将有效数据对象传入数据访问层
63 public string AddStudent(Student student)
64 {
65 //返回信息
66 string message = string.Empty;
67 //学员ID
68 int studentID = 0;
69 studentID =
studentService.GetStudentIDByLoginID(student.LoginId);
70 if(studentID>0)
71 message ="此学员用户名已存在,请更换后重新创建!";
72 else
73 {
74 studentID = studentService.AddSutdent(student);
75 if (studentID > 0)
76 message = "学员账户创建成功!";
77 else
78 message = "学员账户创建失败!";
79 }
80 return message;
81 }
82
83 数据访问层
84 //对实体类对象分析提取参数值 执行SQL
85 public int AddSutdent(Student student)
86 {
87 int number;
88 using (SqlConnection conn = new SqlConnection(connString))
89 {
90 SqlCommand objCommand = new SqlCommand(dboOwner +
".usp_InsertPartStudentInfo", conn);
91 objCommand.CommandType = CommandType.StoredProcedure;
92
93 objCommand.Parameters.Add
("@LoginID", SqlDbType.NVarChar, 50).Value = student.LoginId;
94 objCommand.Parameters.Add
("@LoginPwd", SqlDbType.NVarChar, 50).Value = student.LingPwd;
95 objCommand.Parameters.Add
("@UserStateId", SqlDbType.Int).Value = student.UserStateId;
96 objCommand.Parameters.Add
("@ClassID", SqlDbType.Int).Value = student.ClassID;
97 objCommand.Parameters.Add
("@StudentNO", SqlDbType.NVarChar, 255).Value = student.StudentNO;
98 objCommand.Parameters.Add
("@StudentName", SqlDbType.NVarChar, 255).Value = student.StudentName;
99 objCommand.Parameters.Add
("@Sex", SqlDbType.NVarChar, 255).Value = student.Sex;
100 conn.Open();
101 number = Convert.ToInt32(objCommand.ExecuteScalar());
102 conn.Close();
103
104 }
105 return number;
106 }
107
108 1 public IList<Student> GetAllStudents()
2 {
3 IList<Student> students = new List<Student>();
4 using (SqlConnection conn = new SqlConnection(connString))
5 {
6 SqlCommand objCommand =
new SqlCommand(dboOwner + ".usp_SelectStudentsAll", conn);
7 objCommand.CommandType = CommandType.StoredProcedure;
8 conn.Open();
9 using (SqlDataReader objReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection))
10 {
11 while (objReader.Read())
12 {
13 Student student = new Student();
14 student.LoginId =
Convert.ToString(objReader["LoginId"]);
15 student.StudentNO =
Convert.ToString(objReader["StudentNO"]);
16 student.StudentName =
Convert.ToString(objReader["StudentName"]);
17 student.Sex =
Convert.ToString(objReader["Sex"]);
18 student.StudentIDNO =
Convert.ToString(objReader["StudentIDNO"]);
19 student.Phone =
Convert.ToString(objReader["Phone"]);
20 students.Add(student);
21 }
22 }
23 conn.Close();
24 conn.Dispose();
25 }
26 return students;27 }
上一页 [1] [2] [3]