【前言】:
以蜗牛的速度终于敲完登录这一条线了,终于思路通了一些,但是当自己再敲注册时,又有点卡壳了,我是将原来的学生表分成两个表--学生表和卡表,但是传参数时又不知道咋传了,通过跟小伙伴们交流和自己上网的搜索,终于有了一些思路,然后自己独立完成的第一条线,还是比较有成就感的呢,嘿嘿!
【内容】:
第一步:进行实体层的封装,将数据库表中的内容,写入实体,方便各层的调用,代码很简单,这里就不赘述了!
第二步;创建接口方法,方便D层的实现接口
using LoginEntity;
namespace IDAL
{
public interface IRegister
{
DataTable Registercard(LoginEntity.Card card, LoginEntity.Student student);
}
}
第三步:D层实现接口,然后D层会调用sqlHlper,代码如下:
using System.Data; //引入数据
using System.Data.SqlClient;
using LoginEntity;
using IDAL; //声明接口
namespace DAL
{
public class SqlSeverRegisterDAL:IDAL.IRegister
{
public DataTable Registercard(LoginEntity.Card card, LoginEntity.Student student)
{
SqlParameter []sqlParameter={new SqlParameter ("@CardNo",card .CardNo ),
new SqlParameter ("@StudentNo",student .StudentNo ),
new SqlParameter ("@date",card .date ),
new SqlParameter ("@time",card.time ),
new SqlParameter ("@cash",card.cash),
new SqlParameter ("@status",card.status),
new SqlParameter ("@type",card .type ),
new SqlParameter ("@Ischeck",card.Ischeck),
new SqlParameter("@Explain",card.Explain),
new SqlParameter("@userID",card.UserID)};
string sql="Insert into Card_Info(CardNo,StudentNo,date,time,cash,status,type,Ischeck,UserID,Explain)values(@CardNo,@StudentNo,@date,@time,@cash,@status,@type,@Ischeck,@userID,@Explain)";
DataTable table = SQLHelper.SqlHelper.GetDataTable(sql, CommandType.Text, sqlParameter);
if (table.Rows .Count !=0 ) //判断返回的table表不为空
{
SqlParameter []parameter ={new SqlParameter ("@studentnoo",student .StudentNo ),
new SqlParameter ("@studentname",student .StudentName),
new SqlParameter ("@sex",student.Sex),
new SqlParameter ("@department",student .Department),
new SqlParameter ("@grade",student.Grade),
new SqlParameter ("@class",student .Class)};
string Asql = "Insert into [Student_Info](StudentNo,StudentName,Sex,Department,Grade,Class)valus(@studentno,@studentname,@sex,@department,@grade,@class)";
DataTable table1 = SQLHelper.SqlHelper.GetDataTable(Asql, CommandType.Text, parameter);
return table1;
}
return table;
}
}
}
using System.Configuration;//引用配置文件
using System.Reflection;//引用反射
using IDAL;//引用接口
namespace Factory
{
public class RegisterFatory
{
public static readonly string AssemblyName = "DAL";
public static readonly string db = ConfigurationManager.AppSettings["DB"];//读取配置文件
public IRegister Createcard()
{
string className = AssemblyName + "." + db + "RegisterDAL";
return (IRegister)Assembly.Load(AssemblyName).CreateInstance(className);
}
}
}
第五步、B层调用抽象工厂和接口的方法,并进行逻辑判断;
using System.Data;
using System.Data.SqlClient;
using Factory;
using IDAL;
using LoginEntity;
using System.Windows.Forms; //显示Messagebox控件
namespace LoginBLL
{
public class RegisterBLL
{
//B层实现抽象工厂和接口的方法,然后在这里进行逻辑判读
public DataTable Registercard(Card card, Student student)
{
Factory.RegisterFatory factory = new Factory.RegisterFatory();
IDAL.IRegister Iregister = factory.Createcard();
DataTable userbacktable = Iregister.Registercard(card ,student );
return userbacktable;
using LoginBLL;
using LoginEntity;
using System.Data;
namespace Facade
{
public class RegisterFacade
{
public DataTable Registercard(LoginEntity .Card card,LoginEntity .Student student)
{
//调用B层方法,然后将U层数据传入B层或者返回信息到U层
LoginBLL .RegisterBLL loginBll=new LoginBLL.RegisterBLL();
DataTable dt=loginBll .Registercard (card,student );
return dt ;
}
}
}
using System.Windows.Forms;
namespace LoginUI
{
public partial class RegisterUI : Form
{
public RegisterUI()
{
InitializeComponent();
}
private void btnSave_Click(object sender, EventArgs e)
{
//判读信息完整性
if (txtCardno.Text ==""|| txtStudentno.Text ==""||txtName .Text ==""||cmbSex.Text ==""||txtDepartment.Text ==""||txtClass.Text ==""||txtGrade.Text ==""||cmbStatus.Text ==""||cmbType .Text ==""||txtNotes .Text =="")
{
MessageBox.Show("请将信息补充完整");
}
else
{
LoginEntity.Card card = new LoginEntity.Card(); //实例化实体卡
LoginEntity.Student student = new LoginEntity.Student();//实例化学生表
//将U层从数据传入到实体,然后通过外观层进行传参;
card.CardNo = int.Parse (txtCardno.Text.Trim ()); //类型的强制转换
card.status = cmbStatus.Text;
card.type = cmbType.Text;
card.Ischeck = "未结账";
card.Explain = txtNotes.Text;
card.cash = txtCash.Text;
card.UserID = Form1.LoginTxtUserID.ToString();
string time = DateTime.Now.ToLongTimeString().ToString();
card.time = time;;
string date = DateTime.Now.ToLongDateString().ToString();
card.date = date;
student.StudentNo = txtStudentno.Text;
student.Grade = txtGrade.Text;
student.Department = txtDepartment.Text;
student.Class = txtClass.Text;
student.Sex = cmbSex.Text;
student.StudentName = txtName.Text;
Facade.RegisterFacade facade = new Facade.RegisterFacade(); //实例化外观层
DataTable reg = new DataTable();
reg = facade.Registercard(card ,student); //将实体中的参数传入外观层
}
【总结】:
基本上思路是这样的,万事开头难,当自己真正着手去做,然后进行一些小挫折后,就会真正的明白,会发现其实都是套路呢。开头比较慢,相信自己慢慢就会飞起来的,嘿嘿!以上过程是小编的一些理解,如有疑议,欢迎沟通交流!