【C#】机房重构 --注册

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hdy14/article/details/82555327

#前言
注册敲了有两个星期,终于成功的敲出来了,非常开心!

问了很多人,大家都说是按照登录的套路敲就可以,而实际上,注册还是和登录不一样的

1.登录是在数据库里找数据,sql语句是:执行的是“查”

 
 SELECT * FROM Card_Info WHERE cardNo=@cardNo 

2.注册是先查数据,然后在数据库里添加数据。


Insert into User_Info(userID,password,Level) Values(@userID,@password,@Level)


#正文
弄了一个秋天的背景色嘻嘻

##

IDAL层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Entity;
using System.Data.SqlClient;

namespace IDAL
{
    public interface RegisterIDAL
    {
        DataTable addStudent(Entity.Student_Info studentinfo);
        DataTable addCardNo(Entity.Card_Info cardinfo);
        DataTable addUserID(Entity.User_Info userinfo);

        DataTable Selectstudent(Entity.Student_Info student);
        DataTable SelectUserID(Entity.User_Info user);
        DataTable SelectCardNo(Entity.Card_Info card);


    }
}

##

DAL层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entity;
using System.Data;
using IDAL;
using System.Data.SqlClient;


namespace DAL
{
    public class RegisterDAL : IDAL.RegisterIDAL
    {
        /// <summary>
        /// 插入学生表
        /// </summary>
        /// <param name="studentinfo"></param>
        /// <returns>DataTable</returns>
        public DataTable addStudent(Entity.Student_Info studentinfo)//查找学生表
        {

            DAL.LoginSQLHelper sqlhelper = new DAL.LoginSQLHelper();//实例化helper层
            //传参
            SqlParameter[] sqlParams = { new SqlParameter("@stuNo", studentinfo.stuNo),
                                         new SqlParameter("@sex", studentinfo.sex),
                                         new SqlParameter("@grade", studentinfo.grade),
                                         new SqlParameter("@stuName", studentinfo.stuName)
                                         };//new SqlParameter("@stuName", studentinfo.stuName); }//传参


            //string sql = "SELECT *FROM [Student_Info] WHERE stuNo = @stuNo ";//查询语句
            string sql = "Insert into Student_Info(StuNo,stuName,sex,grade) Values(@stuNo,@stuName,@sex,@grade)";

            DataTable table = sqlhelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
            return table;

        }
        /// <summary>
        /// 插入卡表
        /// </summary>
        /// <param name="cardinfo"></param>
        /// <returns></returns>
        public DataTable addCardNo(Entity.Card_Info cardinfo)//查找卡表
        {
            DAL.LoginSQLHelper sqlhelper = new DAL.LoginSQLHelper();//实例化helper层
            SqlParameter[] sqlParams = { new SqlParameter("@CardNo", cardinfo.CardNo) };
            SqlParameter[] sqlParams1 = { new SqlParameter("@cardstatus", cardinfo.cardStatus) };

            //string sql = "SELECT *FROM[Card_Info] WHERE CardNo = @CardNo ";//查询语句
            string sql = "Insert into Card_Info(cardNo,userID,registerDate,balance,type,cardStatus) Values(@cardNo,@userID,@registerDate,@balance,@type,@cardStatus)";
            DataTable table = sqlhelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
            return table;
        }

        //public DataTable selectUser(Entity.User_Info userinfo)//查找用户表
        //{

        //}
        /// <summary>
        /// 插入用户表
        /// </summary>
        /// <param name="userinfo"></param>
        /// <returns></returns>
        public DataTable addUserID(User_Info userinfo)
        {
            DAL.LoginSQLHelper sqlhelper = new DAL.LoginSQLHelper();//实例化helper层
            //SqlParameter[] sqlParams = { new SqlParameter("@userID", userinfo.userID) };
            SqlParameter[] sqlParams = { new SqlParameter("@Level", userinfo.Level) };

            string sql = "Insert into User_Info(userID,password,Level) Values(@userID,@password,@Level)";
            //string sql = "SELECT *FROM[User_Info] WHERE userID = @userID";//查询语句
            DataTable table = sqlhelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
            return table;
        }

        /// <summary>
        /// 查找学生表,是否完成注册
        /// </summary>
        /// <param name="studentinfo"></param>
        /// <returns></returns>
        public DataTable Selectstudent(Student_Info studentinfo)//查找学生表
        {

            DAL.LoginSQLHelper sqlhelper = new DAL.LoginSQLHelper();//实例化helper层
            //传参
            SqlParameter[] sqlParams = { new SqlParameter("@stuNo", studentinfo.stuNo),
                                         };//new SqlParameter("@stuName", studentinfo.stuName); }//传参


            //string sql = "SELECT *FROM [Student_Info] WHERE stuNo = @stuNo ";//查询语句
            string sql = "SELECT * FROM Student_Info WHERE stuNo=@stuNo ";

            DataTable table = sqlhelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
            return table;

        }
        


        /// <summary>
        /// 查找卡表
        /// </summary>
        /// <param name="cardinfo"></param>
        /// <returns></returns>
        public DataTable SelectCardNo(Card_Info cardinfo)//查找学生表
        {

            DAL.LoginSQLHelper sqlhelper = new DAL.LoginSQLHelper();//实例化helper层
            //传参
            SqlParameter[] sqlParams = { new SqlParameter("@cardNo", cardinfo.CardNo),
                                         };//new SqlParameter("@stuName", studentinfo.stuName); }//传参


            //string sql = "SELECT *FROM [Student_Info] WHERE stuNo = @stuNo ";//查询语句
            string sql = "SELECT * FROM Card_Info WHERE cardNo=@cardNo ";

            DataTable table = sqlhelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
            return table;
        }

        public DataTable SelectUserID(User_Info userinfo)//查找学生表
        {

            DAL.LoginSQLHelper sqlhelper = new DAL.LoginSQLHelper();//实例化helper层
            //传参
            SqlParameter[] sqlParams = { new SqlParameter("@userID", userinfo.userID ),
                                         };//new SqlParameter("@stuName", studentinfo.stuName); }//传参


            //string sql = "SELECT *FROM [Student_Info] WHERE stuNo = @stuNo ";//查询语句
            string sql = "SELECT * FROM User_Info WHERE userID=@userID ";

            DataTable table = sqlhelper.ExecuteQuery(sql, sqlParams, CommandType.Text);
            return table;
        }


    }

}


##

Factory层

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using IDAL;
using System.Reflection;
using System.Data.SqlClient;
using System.Data;

namespace Factory
{
    public class RegisterFactory
    {
        string strDB = ConfigurationManager.AppSettings["DB"];
        public IDAL.RegisterIDAL CreatrUser()
        {
            string ClassName = strDB + "." + "RegisterDAL";

            return (RegisterIDAL)Assembly.Load(strDB).CreateInstance(ClassName);//反射技术的应用
        }

    }
}

##

BLL层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Factory;
using System.Data;
using Entity;
using IDAL;

namespace BLL
{
    public class RegisterBLL
    {
        /// <summary>
        /// 查找学生表
        /// </summary>
        /// <param name="studentinfo"></param>
        /// <returns></returns>
        public bool Selectstudent(Entity.Student_Info studentinfo)
        {
            Factory.RegisterFactory fact = new Factory.RegisterFactory();//实例化工厂
            IDAL.RegisterIDAL idal = fact.CreatrUser();
            DataTable table = idal.Selectstudent(studentinfo);
            //DataTable table1 = idal.selectCardNo(cardinfo);
            //DataTable table2 = idal.selectUserID(userinfo) 
            bool flag;

            if (table.Rows.Count == 0)
            {
                flag = false;

            }
            else
            {
                flag = true;
            }
            return flag;
        }
        /// <summary>
        /// 在学生表增加数据
        /// </summary>
        /// <param name="studentinfo"></param>
        /// <returns></returns>
        public DataTable addStudent(Entity.Student_Info studentinfo)
        {
            Factory.RegisterFactory fact = new Factory.RegisterFactory();//实例化工厂
            IDAL.RegisterIDAL idal = fact.CreatrUser();
            DataTable table = idal.addStudent(studentinfo);
            
            return table;
        }

       
        /// <summary>
        /// 查找卡表
        /// </summary>
        /// <param name="cardinfo"></param>
        /// <returns></returns>
        public bool SelectcardNo(Entity.Card_Info cardinfo)
        {
            Factory.RegisterFactory fact1 = new RegisterFactory();
            IDAL.RegisterIDAL idal1 = fact1.CreatrUser();
            DataTable table = idal1.SelectCardNo(cardinfo);
            bool flag;

            if (table.Rows.Count == 0)
            {
                flag = false;

            }
            else
            {
                flag = true;
            }
            return flag;
        }
        public DataTable addCardNo(Entity.Card_Info cardinfo)
        {
            Factory.RegisterFactory fact = new Factory.RegisterFactory();//实例化工厂
            IDAL.RegisterIDAL idal = fact.CreatrUser();
            DataTable table = idal.addCardNo(cardinfo);

            return table;
        }

        public bool selectUser(Entity.User_Info userinfo)
        {
            Factory.LoginFactory fact = new Factory.LoginFactory();//实例化工厂
            IDAL.LoginIDAL idal = fact.CreateUser();//进入Factory层,调用工厂创建接口;引用了IDAL层
            DataTable table = idal.selectUser(userinfo);//接受D层的返回值; 引用D层
            bool flag;
            //返回值是datatable的类型,如果行数为0 ,则没有符合该账号密码的用户
            if (table.Rows.Count == 0)
            {
                flag = false;
            }

            else
            {
                flag = true;
            }
            return flag; //返回flag
        }

        public DataTable addUserID(Entity.User_Info userinfo)
        {
            Factory.RegisterFactory fact = new Factory.RegisterFactory();//实例化工厂
            IDAL.RegisterIDAL idal = fact.CreatrUser();
            DataTable table = idal.addUserID(userinfo);
             
            return table;
        }

    }
}

##

Facade层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BLL;
using Entity;
using System.Data;


namespace Facade
{
    public class RegisterFacade
    {
        public Boolean Selectstudent(Entity.Student_Info student)
        {
            bool flag;
            BLL.RegisterBLL studentBLL = new BLL.RegisterBLL();//实例化
            flag = studentBLL.Selectstudent(student);
            return flag;//返回flag

        }

        public Boolean SelectcardNo(Entity.Card_Info cardNo)
        {
            bool flag;
            BLL.RegisterBLL CardBLL = new BLL.RegisterBLL();
            flag = CardBLL.SelectcardNo(cardNo);
            return flag;//返回flag
        }

        public Boolean SelectUser(Entity.User_Info user)
        {
            bool flag;
            BLL.LoginBLL userBLL = new BLL.LoginBLL(); //实例化LoginBLL
            flag = userBLL.UserBLL(user);//进入BLL层,并执行操作,返回有用户为true,返回无用户为false.
            return flag;//返回flag
        }

        public DataTable addstudent(Entity.Student_Info student)
        {
            
            BLL.RegisterBLL studentBLL = new BLL.RegisterBLL();//实例化
            DataTable dt = studentBLL.addStudent(student);
            return dt;//返回flag

        }

        
    }
}

##

UI层

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UI层
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }

        private void Register_Load(object sender, EventArgs e)
        {

        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtCardNo.Text.Trim() == "")
            {
                MessageBox.Show("卡号不能为空", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            if (txtGrade.Text.Trim() == "")
            {
                MessageBox.Show("年级不能为空,", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            if (txtRecharge.Text == "")
            {
                MessageBox.Show("充值金额不能为空,", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            if (txtrestMoney.Text == "")
            {
                MessageBox.Show("余额不能为空", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            if (txtSex.Text == "")
            {
                MessageBox.Show("性别不能为空,", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            if (txtStuNo.Text.Trim() == "")
            {
                MessageBox.Show("姓名不能为空", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            if (Level.Text.Trim() == "")
            {
                MessageBox.Show("请选择用户级别", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
            
            
            Facade.RegisterFacade facade = new Facade.RegisterFacade();
            Entity.Student_Info student = new Entity.Student_Info();
            Entity.Card_Info card = new Entity.Card_Info();
            Entity.User_Info user = new Entity.User_Info();


            //学生表
            student.stuName = Convert.ToInt32(txtuserName.Text.Trim());
            student.stuNo = Convert.ToInt32(txtStuNo.Text.Trim());
            student.grade = Convert.ToInt32(txtGrade.Text.Trim());
            student.sex = Convert.ToInt32(txtSex.Text.Trim());

            card.CardNo = Convert.ToInt32(txtCardNo.Text.Trim());
            card.cardStatus  = Convert.ToInt32(txtCardNo.Text.Trim());            
            card.registerMoney = Convert.ToInt32(txtrestMoney.Text.Trim());

            user.Level = Level.Text.Trim();
            
            DataTable dt= facade.addstudent(student);

            Boolean flag = false;
            Facade.RegisterFacade registerFacade = new Facade.RegisterFacade();

            flag = facade.Selectstudent(student);

            if (flag != false)
            {
                MessageBox.Show("注册成功!");
                
            }
            else
            {
               MessageBox.Show("注册失败");
            }



        }

       

        private void btnCancel_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void txtSex_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtGrade_TextChanged(object sender, EventArgs e)
        {

        }

        private void Level_SelectedIndexChanged(object sender, EventArgs e)
        {
            

        }

        private void txtuserName_TextChanged(object sender, EventArgs e)
        { 

        }
    }
}

##

Entity层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Entity
{
    public class Student_Info
    {
        private  int  _stuno;
        public  int stuNo
        {
            get { return _stuno; }
            set { _stuno = value;}
        }

        private int _stuname;
        public int  stuName
        {
            get { return _stuname; }
            set { _stuname = value; }
        }

        private int _age;
        public int age
        {
            get { return _age; }
            set { _age = value;}
        
        }

        private int _sex;
        public int  sex
        {
            get { return _sex; }
            set { _sex = value;}
        }

        private int _class ;
        public int Class
        {
            get { return _class; }
            set { _class = value;}
        }

        private int _explain;
        public int explain
        {
            get { return _explain; }
            set { _explain = value; }
        }

        private int _grade;
        public int grade
        {
            get { return _grade; }
            set { _grade = value; }
        }


        
        
        
    }
}

#后记
不懂的童鞋可私聊我,欢迎~

猜你喜欢

转载自blog.csdn.net/hdy14/article/details/82555327