机房重构之添加用户

简介: 机房重构之添加用户

添加用户也是用到了七层,说来也简单,先分为两部分,首先要对添加的数据加一个判断,看数据库里是否有重复的数据,然后在添加数据。我这里是用卡号来判断是否重复的。如下:


IDAL


DataTable Select(Entity.AddStudent user);//判断是否有相同数据


using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IDAL
{
    public interface IAddstudent
    {
        DataTable Select(Entity.AddStudent user);//判断是否有相同数据
       int  AddStudent(Entity.AddStudent user);//添加数据
    }
}


DAL


public class Add: IDAL.IAddstudent
    {
        #region 判断是否有重复数据
        public DataTable Select(Entity.AddStudent user)
        {
            SQLHelper sqlhelper = new SQLHelper();//实例化SQLhelp
                                                  //定义一个bool型变量,用来接收返回数据
                                                  //bool flag;
            string sql = "select * from student_info where cardno=@cardno";//查询卡号是否重复
            SqlParameter[] sqlpar =
            {
                new SqlParameter("@cardno", user.cardno ),
            };
            //DataTable tabel =sqlhelper .ExecuteQuery(sql,sqlpar)
            DataTable tabel = sqlhelper.ExecuteQuery(sql, sqlpar, CommandType.Text);
            return tabel;
        }
        #endregion


Facade


public class Addstudent
    {
        #region 判断数据是否重复
        public bool select(Entity .AddStudent user)
        {
            BLL.Addstudent userbll = new BLL.Addstudent();
            bool falg;
            falg = userbll.flag(user );
            return falg;
        }
        #endregion


BLL


namespace BLL
{
    public class Addstudent
    {
        #region 判断是否有重复数据
        public bool flag(Entity.AddStudent user)
        {
            Factory.AddStudent fact = new Factory.AddStudent();//实例化工厂
            IDAL.IAddstudent idal = fact.Addstudent();
            DataTable table = idal.Select(user);
            bool flag;
            if (flag= table.Rows.Count == 0)//判断数据库里面是否有重复的数据,如果=0说明没有重复的数据行
            {
                flag = true;
            }
            else
            {
                flag = false;
            }
            return flag;
        }


第二步    往数据库里面添加数据


Entity


namespace Entity
{
    public class AddStudent
    {
        public int  studentID { get; set; }
        public string studentName { get; set; }
        public string pwd { get; set; }
        public string QRpwd { get; set; }
        public string grade { get; set; }
        public string department { get; set; }
        public string classs { get; set; }
        public string money { get; set; }
        public string sex { get; set; }
        public string cardno { get; set; }
    }


IDAL


int  AddStudent(Entity.AddStudent user);//添加数据

namespace IDAL
{
    public interface IAddstudent
    {
        DataTable Select(Entity.AddStudent user);//判断是否有相同数据
       int  AddStudent(Entity.AddStudent user);//添加数据
    }
}


DAL


public int AddStudent(Entity.AddStudent user)
        {
            SQLHelper sqlhelper = new SQLHelper();//实例化SQLhelp
            SqlParameter[] sqlpar =
            {
                    new SqlParameter("@txtStudentID",user.studentID),
                    new SqlParameter("@txtStudentName",user.studentName),
                    new SqlParameter("@txtPassword",user.pwd),
                    //new SqlParameter("@txtQRpassword",user.pwd),
                    new SqlParameter("@comGrade",user.grade),
                    new SqlParameter("@comDeparment",user.department),
                    new SqlParameter("@comClass",user.classs),
                    new SqlParameter("@txtMoney",user.money),
                    new SqlParameter("@comSex",user.sex),
                    new SqlParameter ("@txtcardno",user.cardno)
        };//定义需要传递的参数      
            string sql = @"insert into student_info(cardno,studentNo,studentName,sex,cash,class,department,grade,password)values(@txtcardno,@txtStudentID,@txtStudentName,@comSex,@txtMoney,@comClass,@comDeparment,@comGrade,@txtPassword)";
            //string sql = "insert into student_info(cardno, studentNo, studentName, sex, cash, UserID, status, Ischeck, date, time, type,class,department,grade,password)values(@cardno,44,4,44,53,3,3,3,'2021-04-21','19:49:31',3,5,6,4,2)"
            //DataTable table  = sqlhelper.ExecuteQuery(sql, sqlpar, CommandType.Text);
            int table = sqlhelper.ExecuteNonQuery(sql, sqlpar, CommandType.Text);
            return table ;
        }
    }


Factory


namespace Factory
{
    public class AddStudent
    {
        string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];
        public IAddstudent Addstudent()
        {
            string ClassName = StrDB + "." + "Add";
            return (IAddstudent)Assembly.Load(StrDB).CreateInstance(ClassName);
        }
    }
}


BLL


public int  addstudent(Entity.AddStudent user)//添加数据
        {
            Factory.AddStudent fact = new Factory.AddStudent();//实例化工厂
            IDAL.IAddstudent idal = fact.Addstudent();
            int  flag = idal.AddStudent(user);
            return flag;
        }


Facade


public int  addstudent(Entity.AddStudent user)
        {
            BLL.Addstudent userbll = new BLL.Addstudent();
            int  flag = userbll.addstudent(user);
            return flag;
        }


UI


private void button1_Click(object sender, EventArgs e)
        {
            if (txtStudentID.Text =="")
            {
                MessageBox.Show("学号不能为空!", "提示");
            }
            else if (txtStudentName.Text =="")
            {
                MessageBox.Show("姓名不能为空!", "提示");
            }
            else if (txtPassword.Text =="")
            {
                MessageBox.Show("密码不能为空!", "提示");
            }
            else if (txtQRpassword.Text =="")
            {
                MessageBox.Show("确认密码不能为空!", "提示");
            }
            else if (comGrade.Text =="")
            {
                MessageBox.Show("请选择您所在的年级!", "提示");
            }
            else if (comDeparment.Text =="")
            {
                MessageBox.Show("请选择您所在的系别!", "提示");
            }
            else if (comClass.Text =="")
            {
                MessageBox.Show("请选择您所在的班级!", "提示");
            }
            else if (txtMoney.Text =="")
            {
                MessageBox.Show("请输入您要充值的金额,最小金额不能少于3元!", "提示");
            }
            else if (txtPassword.Text != txtQRpassword.Text )
            {
                MessageBox.Show("密码和确认密码不一致,请您再次确认!", "提示");
            }
            else
            {
                Facade.Addstudent facade = new Facade.Addstudent();
                Entity.AddStudent entity = new Entity.AddStudent();
                entity.studentID = Convert.ToInt32(txtStudentID.Text.Trim());
                entity.studentName = txtStudentName.Text.Trim();
                entity.pwd = txtPassword.Text.Trim();
                entity.QRpwd = txtQRpassword.Text.Trim();
                entity.grade = comGrade.Text.Trim();
                entity.classs = comClass.Text.Trim();
                entity.department = comDeparment.Text.Trim();
                entity.sex = comSex.Text.Trim();
                entity.money = txtMoney.Text.Trim();
                entity.cardno = txtcardno.Text.Trim();
                bool falg = facade.select(entity);
                if (falg ==false)//与B层的判断关联
                {
                    MessageBox.Show("已有此卡号,请重新输入!","提示");
                }
                else
                {
                    facade.addstudent(entity);
                    MessageBox.Show("添加成功","提示");
                }
            }
        }

相关文章
|
JSON JavaScript 数据格式
Vue axios 发送 FormData 请求
Vue axios 发送 FormData 请求
524 0
|
C语言
C语言:数组和指针笔试题解析(包括一些容易混淆的指针题目)
C语言:数组和指针笔试题解析(包括一些容易混淆的指针题目)
174 1
|
10月前
|
安全 编译器 Swift
Swift开发
Swift开发
268 9
|
10月前
|
机器学习/深度学习 人工智能 并行计算
【AI系统】GPU 架构回顾(从2010年-2017年)
自1999年英伟达发明GPU以来,其技术不断革新。本文概述了从2010年至2024年间,英伟达GPU的九代架构演变,包括费米、开普勒、麦克斯韦、帕斯卡、伏特、图灵、安培、赫柏和布莱克韦尔。这些架构不仅在游戏性能上取得显著提升,还在AI、HPC、自动驾驶等领域发挥了重要作用。CUDA平台的持续发展,以及Tensor Core、NVLink等技术的迭代,巩固了英伟达在计算领域的领导地位。
379 1
|
机器学习/深度学习 算法 计算机视觉
基于深度学习网络的USB摄像头实时视频采集与人脸检测matlab仿真
**摘要 (Markdown格式):** ```markdown - 📹 使用USB摄像头(Tttttttttttttt666)实时视频检测,展示基于YOLOv2在MATLAB2022a的实施效果: ``` Tttttttttttttt1111111111------------5555555555 ``` - 📺 程序核心利用MATLAB视频采集配置及工具箱(Dddddddddddddd),实现图像采集与人脸定位。 - 🧠 YOLOv2算法概览:通过S×S网格预测边界框(B个/网格),含坐标、类别概率和置信度,高效检测人脸。
|
Arthas 监控 Java
线上频繁fullgc问题-SpringActuator的坑
偷偷开启的监控在吃内存
261 0
|
人工智能 编译器 vr&ar
动态库和静态库 | AI工程化部署
我们在编写接口或者使用第三方应用时,都会打包或者引入.so或者.a文件。这个so就是动态库,.a文件就是静态库 【1月更文挑战第5天】
278 1
|
存储 人工智能 Cloud Native
创新场景|“耳朵经济”快速增长背后,数据价值是如何释放的?
通过大数据平台+数据中台的建设,喜马拉雅重构了数据底座,真正具备了更稳定、更高效、更智能化的应用数据的能力。
323 0
利用Flutter的LayoutBuilder、BoxConstraints创建自适应布局控件
利用Flutter的LayoutBuilder、BoxConstraints创建自适应布局控件
|
测试技术 Linux 调度
性能测试必备知识(7)- 深入理解“CPU 使用率”
性能测试必备知识(7)- 深入理解“CPU 使用率”
737 0
性能测试必备知识(7)- 深入理解“CPU 使用率”