添加用户也是用到了七层,说来也简单,先分为两部分,首先要对添加的数据加一个判断,看数据库里是否有重复的数据,然后在添加数据。我这里是用卡号来判断是否重复的。如下:
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);//添加数据
}
}
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
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
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;
}
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; }
}
namespace IDAL
{
public interface IAddstudent
{
DataTable Select(Entity.AddStudent user);//判断是否有相同数据
int AddStudent(Entity.AddStudent user);//添加数据
}
}
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 ;
}
}
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);
}
}
}
public int addstudent(Entity.AddStudent user)//添加数据
{
Factory.AddStudent fact = new Factory.AddStudent();//实例化工厂
IDAL.IAddstudent idal = fact.Addstudent();
int flag = idal.AddStudent(user);
return flag;
}
public int addstudent(Entity.AddStudent user)
{
BLL.Addstudent userbll = new BLL.Addstudent();
int flag = userbll.addstudent(user);
return flag;
}
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("添加成功","提示");
}
}
}