1. 程式人生 > >【C#重構】——註冊

【C#重構】——註冊

教師端給學生註冊功能
資料庫worklog_info 表
在這裡插入圖片描述
功能實現:
註冊學號、姓名、狀態、充值金額 點選註冊後卡號自動生成,返回在卡號一欄
在這裡插入圖片描述
U層

        /// <summary>
        /// 點選註冊
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            //判斷是否為空
            if (txtSID.Text.Trim() == ""|| txtName.Text.Trim() == ""|| cmbState.Text.Trim() == ""|| txtRecharge.Text.Trim()=="")
            {
                MessageBox.Show("請把資訊填寫完整", "溫馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            try
            {
                //例項化實體層
                Entity.StudentInfo student = new Entity.StudentInfo();
                student.StudentID= Convert.ToInt32(txtSID.Text.Trim());
                student.Studentname = txtName.Text.Trim();
                student.Status = cmbState.Text.Trim();
                student.Balance = Convert.ToInt32(txtRecharge.Text.Trim());

                //外觀層例項化
                //查詢資料庫中學號是否註冊過
                DataTable table;
                Boolean flag;
                Facade.StudentFacade facade = new Facade.StudentFacade();
                flag = facade.SelectStudentIDFacade(student);
                
                //如果沒有被註冊
                if (flag!=false)
                {
                    //把註冊資訊插入資料庫表
                    table = facade.InsterStudentFacade(student);
                    DataTable table1 = facade.SelectUserIDFacade(student);
                    //插入一行後不為空,卡號返回
                    if (table1.Rows.Count != 0)
                    {
                        student.UserID = Convert.ToInt32(table1.Rows[0][0].ToString().Trim());
                        txtCardNO.Text = Convert.ToString (student.UserID);
                        MessageBox.Show("卡號註冊成功!", "溫馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        MessageBox.Show("找不到卡號請聯絡管理員!", "溫馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    MessageBox.Show("學號已經註冊過!", "溫馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Facade層

public  class RechargeFacade
    {
        /// <summary>
        /// 查詢student表,核對學號是否被註冊
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        public DataTable SelectStudentFacade(Entity.StudentInfo student)
        {
            BLL.RechargeBLL bll = new BLL.RechargeBLL();
            DataTable table = bll.SelectStudnetBLL(student);
            return table;
        }
        /// <summary>
        /// 插入註冊資訊入表
        /// </summary>
        /// <param name="money"></param>
        /// <returns></returns>
        public DataTable InsertRechargeFacade(Entity.RechargeInfo money)
        {
            BLL.RechargeBLL bll = new BLL.RechargeBLL();
            DataTable table = bll.InsertRechargeBLL(money);
            return table;
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        public DataTable UpdateStudentFacade(Entity.StudentInfo student)
        {
            BLL.RechargeBLL bll = new BLL.RechargeBLL();
            DataTable table = bll.UpdateStudentBLL(student);
            return table;
        }
    }

BLL層

public class RechargeBLL
    {
        public DataTable SelectStudnetBLL(Entity.StudentInfo student)
        {
            Factory.RechargeFactory factory = new Factory.RechargeFactory();
            IDAL.IRechargeIDL dll = factory.CreateUser();
            DataTable table = dll.SelectStudent(student);
            return table;
        }
        public DataTable InsertRechargeBLL(Entity.RechargeInfo money)
        { 
            Factory.RechargeFactory factory = new Factory.RechargeFactory();
            IDAL.IRechargeIDL dll = factory.CreateUser();
            DataTable table = dll.InsertRecharge(money);
            return table;
        }

        public DataTable UpdateStudentBLL(Entity.StudentInfo student)
        {
            Factory.RechargeFactory factory = new Factory.RechargeFactory();
            IDAL.IRechargeIDL dll = factory.CreateUser();
            DataTable table = dll.UpdateStudent(student);
            return table;
        }
    }

DAL層

public class RechargeDAL:IDAL.IRechargeIDL
    {
        /// <summary>
        /// 查詢學生表的初始註冊錢+學生註冊狀態
        /// </summary>
        /// <param name="student"></param>
        /// <returns></returns>
        public DataTable SelectStudent(Entity.StudentInfo student)
        {
            SQLHelper sqlHelper = new SQLHelper();
            SqlParameter[] sqlParams = { new SqlParameter("@UserID", student.UserID) };
            String sql = "Select * from student_info where userID [email protected] ";
            DataTable table = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
            return table;
        }
        public DataTable InsertRecharge(Entity.RechargeInfo money)
        {
            DateTime date = DateTime.Now;
            SQLHelper sqlHelper = new SQLHelper();
            SqlParameter[] sqlParams = { new SqlParameter("@CardNO",money.UserID),
                                         new SqlParameter("@Date",date.ToShortDateString()),
                                         new SqlParameter("@Status",money.Status),
                                         new SqlParameter("@Addmoney",money.Addmoney)};
            String sql = "Insert into recharge_info(userID,addmoney,date,status) Values(@CardNO,@Addmoney,@Date,@Status)";
            DataTable table = sqlHelper.ExecuteNonQuery(sql,sqlParams,CommandType.Text);
            return table;
        }

        public DataTable UpdateStudent(Entity.StudentInfo student)
        {
            SQLHelper sqlHelper = new SQLHelper();
            SqlParameter[] sqlParams = { new SqlParameter("@Balance",student.Balance)};
            String sql = "Update student_info Set balance [email protected] ";
            DataTable table = sqlHelper.ExecuteNonQuery(sql,sqlParams,CommandType.Text);
            return table;
        }
    }