**三层结构 实体类

  1 用户接口层
  2 //构建实体对象student并赋值
  3 private void btnSave_Click(object sender, EventArgs e)
  4         {
  5             //验证是否输入了必要的信息
  6             if (txtUserName.Text.Equals(String.Empty))  // 验证是否输入了用户名   
  7             {
  8                 MessageBox.Show("请输入用户名""输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  9                 return;
 10             }
 11             if (txtPassword.Text.Equals(String.Empty))  // 验证是否输入了密码
 12             {
 13                 MessageBox.Show("请输入密码""输入提示"
                                     MessageBoxButtons.OK, MessageBoxIcon.Information);
 14                 return;
 15             }
 16             if (txtPswAgain.Text.Equals(String.Empty))  // 验证是否输入了确认密码
 17             {
 18                 MessageBox.Show("请输入确认密码""输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 19                 return;
 20             }
 21             if (!txtPassword.Text.Equals(txtPswAgain.Text))  // 验证两次密码是否一致
 22             {
 23                 MessageBox.Show("两次输入的密码不一致""输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 24                 return;
 25             }
 26             if (!radActive.Checked && !radInactive.Checked)  // 验证是否选择了用户状态
 27             {
 28                 MessageBox.Show("请设置用户的状态""输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 29                 return;
 30             }
 31             if (txtName.Text.Equals(String.Empty))  // 验证是否输入了姓名
 32             {
 33                 MessageBox.Show("请输入用户姓名""输入提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 34                 return;
 35             }
 36             if (cboClass.Text.Equals(String.Empty))  // 验证是否选择了用户的班级
 37             {
 38                 MessageBox.Show("请输入选择用户班级""输入提示", MessageBoxButtons.OK, 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         } 

posted @ 2008-10-21 22:59  Edward Xie  阅读(431)  评论(0编辑  收藏  举报