C#机房重构之注册窗体

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

前言

在敲代码之前画出自己想要的流程图,从一下几点来描述一下我的注册。
我是以数据库表来建立类库的,也可以以功能,以增删改查等。

注意

1.数据库中有两个表涉及到注册,分别是学生表和user表。
注册学生同时,也要将信息放到user表。因为我们是在user表中判断登陆权限的。

2学生表中的权限可以直接设定为一般用户,不需要选择。

3注册时并没有给学生输入密码。这个在注册到user表中可以给学生的密码设定一个初始密码666666。
学生窗体中有修改密码窗体,可以将初始密码改成自己想要的。
数据库图
4注册窗体中可以放一个按钮,作为充值金额。

5注册前,要查询注册的账号是否存在,如果存在,则弹框。
如果不存在,依次将数据存到student表和user表。
最后判断存盘是否成功。

这里写图片描述

代码

Entity

public class StuInfo         //学生表
    {
        public string UserID { set; get; }
        public string studentName { set; get; }
        public string Sex { set; get; }
        public string Department { set; get; }
        public string Grade { set; get; }
        public string Class { set; get; }
        public decimal Cash { set; get; }
        public string Type { set; get; }
        public string Explain { set; get; }
        public string Ischeck { set; get; }
    }
public class userinfo         //--------user表
    {
        public string UserID { get; set; }
        public string PWD { get; set; }
        public string UserName { get; set; }
        public string Level { get; set; }
        public string state { get; set; }

    }

IDAL

//学生表
        DataTable selectStu(Entity.StuInfo StuInfo);//查询ID是否存在
        int addStu(Entity.StuInfo stu);//添加学生
//user表
        DataTable selectUserBLL(Entity.userinfo userinfo);//查询ID是否存在
        //-------------------------------------------------------------
        int addUser(Entity.userinfo userinfo);//添加用户到user表

DAL

//学生表
    public class StuDAL : IDAL.StuIDAL //实现学生注册的接口
    {
        #region//查询学生表里是否存在ID号-----------------------------------------------------------------

        public DataTable selectStu(Entity.StuInfo StuInfo)
        {

            SQLHelper sqlHelper = new SQLHelper();   //实例化SQLHelper

            //查询语句

            SqlParameter[] sqlParams = { new SqlParameter("@UserID",StuInfo.UserID ) }; //需要引用sqlclient;参数

            string sql = @"SELECT *FROM [Student] WHERE UserID=@UserID";

            DataTable table = sqlHelper.ExecuteQuery(sql, sqlParams, CommandType.Text); //ExecuteQuery查询

            return table;
        }
        #endregion

        #region//向学生表里添加信息--------------------------------------------------------------


        public int addStu(Entity.StuInfo stu)
        {
            SQLHelper sqlHelper = new SQLHelper();

            //   向表中插入信息
            string sql = "INSERT INTO Student(UserID,studentName,Sex,Department,Grade,Class,Cash,Type,Explain,Ischeck)values(@UserID,@studentName,@Sex,@Department,@Grade,@Class,@Cash,@Type,@Explain,@Ischeck)";

            SqlParameter[] sqlParams = new SqlParameter[]
            {
                new SqlParameter ("@UserID",stu.UserID ),
                new SqlParameter ("@studentName",stu.studentName),
                new SqlParameter ("@Sex",stu.Sex ),
                new SqlParameter ("@Department",stu.Department ),
                new SqlParameter ("@Grade",stu.Grade ),
                new SqlParameter ("@Class",stu.Class),
                new SqlParameter ("@Cash",stu.Cash ),
                new SqlParameter ("@Type",stu.Type ),
                new SqlParameter ("@Explain",stu.Explain ),
                new SqlParameter ("@Ischeck",stu.Ischeck ),
            };

            int result = sqlHelper.ExecuteNonQuery(sql, sqlParams, CommandType.Text);
            return result;
        }


        #endregion
#region//查询user表里是否存在ID号


        public DataTable selectUserBLL(Entity.userinfo userinfo)
        {
            SQLHelper sqlHelper = new SQLHelper();  //实例化sqlhelper

            //引用
            SqlParameter[] sqlParams = { new SqlParameter("@UserID", userinfo.UserID) };  //user表里有没有这个ID号

            string sql = @"SELECT *FROM [User_Info] WHERE UserID = @UserID";  //查表

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

            return table;

        }
        #endregion

        //---------------------------------------------------------

#region//向user表里添加学生信息

        public int addUser(Entity.userinfo userinfo)
        {
            SQLHelper sqlHelper = new SQLHelper();  //实例化SQLhelper

            string sql = "INSERT INTO User_Info(UserID,PWD,UserName,Level,State)VALUES(@UserID,@PWD,@UserName,@Level,@State)"; //插入语句

            SqlParameter[] sqlParams = new SqlParameter[]    //添加信息,赋值
            {
                new SqlParameter ("@UserID",userinfo .UserID ),
                new SqlParameter ("@PWD",userinfo .PWD ),
                new SqlParameter ("@UserName",userinfo.UserName ),
                new SqlParameter ("@Level",userinfo .Level ),
                new SqlParameter ("@State",userinfo.state ),
            };

            int result = sqlHelper.ExecuteNonQuery(sql, sqlParams, CommandType.Text);      //插入
            return result;
        }

Factory

//学生
        public IDAL.StuIDAL Student()
        {

            string ClassName = StrDB + "." + "StuDAL";    //DAL层的类名

            return (IDAL.StuIDAL)Assembly.Load(StrDB).CreateInstance(ClassName);  //反射+工厂的应用
        }
        #endregion
//user
        public IDAL.UserIDAL User()
        {
            //DAL层的类名
            string ClassName = StrDB +"." + "UserDAL";
            //反射加工厂的应用
            return (IDAL.UserIDAL)Assembly.Load(StrDB).CreateInstance(ClassName);
        }
        #endregion

BLL

//学生表:
        #region//查询学生是否存在
        public DataTable selectStuBLL(Entity.StuInfo StuInfo)
        {
            Factory.LoginFactory fact = new Factory.LoginFactory(); //实例化工厂

            IDAL.StuIDAL idal = fact.Student();                     //调用工厂方法创建接口

            DataTable table = idal.selectStu(StuInfo);              //接受D层返回值

            return table;
        }
        #endregion


        #region//添加学生到学生表

        public Boolean addStu(Entity.StuInfo StuInfo)
        {

            Factory.LoginFactory fact = new Factory.LoginFactory();//实例化工厂

            IDAL.StuIDAL idal = fact.Student();//调用工厂方法创建接口

            int result = idal.addStu(StuInfo);//接受D层返回值

            bool flag;

            if (result == 0)
            {
                flag = false;
            }
            else
            {
                flag = true;
            }
            return flag;
        }
//user表
        #region//查询用户是否存在

        public DataTable selectUserBLL(Entity.userinfo userinfo)
        {
            Factory.LoginFactory fact = new Factory.LoginFactory(); //实例化工厂

            IDAL.UserIDAL idal = fact.User(); //调用工厂方法创建接口

            DataTable table = idal.selectUserBLL(userinfo);  //接收D层返回值

            return table;
        }

        #endregion

        #region//添加学生到user表

        public Boolean addUser(Entity.userinfo userinfo)
        {
            Factory.LoginFactory fact = new Factory.LoginFactory();

            IDAL.UserIDAL idal = fact.User();

            int result = idal.addUser(userinfo);
            bool userflag;
            if (result ==0)
            {
                userflag = false;   
            }
            else
            {
                userflag = true;
            }
            return userflag;

        }
        #endregion

        #endregion

Facade

//学生表
     public class StuFacade
    {
        #region//查找
        public DataTable selectStu(Entity.StuInfo StuInfo)
        {
            DataTable flag;
            StuBLL stuBLL = new StuBLL();
            flag = stuBLL.selectStuBLL(StuInfo);
            return flag;
        }
        #endregion


        #region//添加到学生表

        public Boolean addStu(Entity.StuInfo StuInfo)
        {
            bool flag;
            StuBLL stuBLL = new StuBLL();
            flag = stuBLL.addStu(StuInfo);
            return flag;
        }

        #endregion
//user表
//查找是否存在

        public DataTable selectaddUser(Entity.userinfo userinfo)
        {
            DataTable userflag;
            UserBLL userBLL = new UserBLL();
            userflag = userBLL.selectUserBLL(userinfo);
            return userflag;

        }
//添加信息到user表

        public Boolean addUser(Entity.userinfo userinfo)
        {
            bool userflag;
            UserBLL userBLL = new UserBLL();
            userflag = userBLL.addUser(userinfo);
            return userflag;
        }

UI

//注册
        private void buttonok_Click(object sender, EventArgs e)
        {

            //判空--------代码省略

            if (txtUserID.Text == "")
            {
                MessageBox.Show("请输入账号","提示",MessageBoxButtons .OK ,MessageBoxIcon.Warning );
                txtUserID.Focus();
            }

            else if (txtExplain.Text == "") ---------数据库中备注是可为null的
            {
                txtExplain.Text = "无";
            }

            else
            {
                //判存学生表与user表是否存在ID,没有则将注册信息插入到表中

                try
                {

                    Facade.StuFacade Facade = new Facade.StuFacade();//实例化学生外观

                    Entity.StuInfo stu = new Entity.StuInfo();      //实例化用户

                    stu.UserID = txtUserID.Text.Trim();


                    DataTable flag = Facade.selectStu(stu);

                    //--------------------------------user

                    Facade.UserFacade facade = new Facade.UserFacade();//实例化user外观

                    Entity.userinfo UserInfo = new Entity.userinfo(); //-----------实例化user实体

                    UserInfo.UserID = txtUserID.Text.Trim();
                    DataTable userflag = facade.selectaddUser(UserInfo);


                    if (flag.Rows.Count != 0||userflag .Rows.Count !=0)
                    {
                        MessageBox.Show("用户已存在", "温馨提示");
                        txtUserID.Focus();
                    }
                    else
                    {

                  //将信息添加到学生表中

                        stu.UserID = txtUserID.Text.Trim();
                        stu.studentName = txtstudentName.Text.Trim();
                        stu.Sex = cmbSex.Text.Trim();
                        stu.Department = txtDepartment.Text.Trim();
                        stu.Grade = txtGrade.Text.Trim();
                        stu.Class = txtClass.Text.Trim();
                        stu.Cash = Convert.ToDecimal (txtCash.Text.Trim());
                        stu.Type = "一般用户";
                        stu.Explain = txtExplain.Text.Trim();
                        stu.Ischeck = "未结账";

                //将信息添加到user表

                        UserInfo.UserID = txtUserID.Text.Trim();
                        UserInfo.PWD = "666666";
                        UserInfo.UserName = txtstudentName.Text.Trim();
                        UserInfo.Level = "一般用户";
                        UserInfo.state = "正在使用";

                //查看是否插入成功

                        bool addflag = Facade.addStu(stu);
                        bool userflag1 = facade.addUser(UserInfo);

                        if (addflag == false||userflag1 ==false)
                        {
                            MessageBox.Show("注册失败");
                        }
                        else
                        {
                            MessageBox.Show("注册成功!");
                            Clear clear = new Clear(this);
                        }
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
                #endregion

        }

设置combo:

    private void frmRegister_Load(object sender, EventArgs e)
    {
       cmbSex.Items.Add("男");
       cmbSex.Items.Add("女");
    }

错误集锦

1将截断字符串或二进制数据

这里写图片描述

解析:这个是因为数据库里面的字段设置与输入的内容冲突。
sex这个combo选择是“男”“女”,我将数据类型设置的char(1)。但是一个汉字占两个字节,所以数据库的存储不够,就报错了。
解决:改为char(2)就可以了。
这里写图片描述


2.类型当作变量来使用
解决:将sqlParameter[0]改为sqlParams[0]就可以了。这里的sqlParams是变量,而sqlParameter是类型。
这里写图片描述


3.字段初始值无法引用非静态字段

这里写图片描述

这三句代码是写在方法里的,但是这里直接是在类里。少了一行代码。

public DataTable selectStuBLL(Entity.StuInfo StuInfo)

原代码应该为:

public class StuBLL
    {
        //查询学生是否存在
        public DataTable selectStuBLL(Entity.StuInfo StuInfo)
        {
            Factory.LoginFactory fact = new Factory.LoginFactory(); //实例化工厂

            IDAL.StuIDAL idal = fact.Student();                     //调用工厂方法创建接口

            DataTable table = idal.selectStu(StuInfo);              //接受D层返回值

            return table;
        }
    }

这个一看就是照着葫芦画瓢,没有逻辑思维。所以在敲代码之前,一定要先捋顺了脑子。

后记

分享到这,另:附属一个一键清空的方法。一键清空

猜你喜欢

转载自blog.csdn.net/Carrie_Q/article/details/81985538