UI层
namespace UI { public partial class Recharge : Form { public Recharge() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { Entity.RechargeInfo rechargeInfo = new Entity.RechargeInfo();//实例化充值层 Facade.RechargeFacade faca = new Facade.RechargeFacade();//实例化外观层 //定义需要插入到充值表当中的数据 rechargeInfo.CardNo = CardNo.Text.Trim(); rechargeInfo.addmoney = AddMoney.Text.Trim(); rechargeInfo.state = "未结账"; rechargeInfo.date = DateTime.Now.ToShortDateString(); rechargeInfo.time = DateTime.Now.ToShortTimeString(); rechargeInfo.UserID =LoginUser.UserID; Entity.StudentInfo student = new Entity.StudentInfo();//实例化学生 // Facade.StudentFacade facade = new Facade.StudentFacade(); //实例化学生外观 student.CardNo = CardNo.Text.Trim(); student.state = "使用"; Facade.RechargeFacade Facade = new Facade.RechargeFacade();//实例化学生外观 Entity.StudentInfo StudentInfo = new Entity.StudentInfo(); StudentInfo.CardNo = CardNo.Text.Trim(); DataTable dtCard = Facade.InquiryCardInfo(StudentInfo); if (dtCard.Rows.Count != 0) { rechargeInfo.studentNo = dtCard.Rows[0][1].ToString(); } if (dtCard.Rows.Count == 0) { MessageBox.Show("卡号不存在", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } bool rechargecard = faca.addrecharge(rechargeInfo);//调用插入充值记录方法 if (rechargecard) { StudentInfo.CardNo = CardNo.Text.Trim(); StudentInfo.cash = AddMoney.Text.Trim(); int balanceNow = Convert.ToInt32(dtCard.Rows[0][6]) + Convert.ToInt32(AddMoney.Text.Trim()); StudentInfo.cash = Convert.ToString(balanceNow); int dt1 = Facade.updateStudentCash(StudentInfo); MessageBox.Show("充值成功"); label1.Text = Convert.ToString(balanceNow); } else { MessageBox.Show("充值失败"); return; } } } }
D层
namespace DAL { public class RechargeDAL : IDAL.RechargeIDAL { public int addrechargeInfo(RechargeInfo reCharge) { SQLHelper sqlHelper = new SQLHelper(); //实例化SQLhelper string sql = "INSERT INTO ReCharge(cardno,studentNo,addmoney,date,time,UserID,state)VALUES(@cardNo,@studentNo,@addmoney,@date,@time,@UserID,@state)"; //插入语句 SqlParameter[] sqlParams = new SqlParameter[] //添加信息,赋值 { new SqlParameter ("@studentNo",reCharge.studentNo), new SqlParameter ("@cardNo",reCharge.CardNo), new SqlParameter ("@addmoney",reCharge.addmoney), new SqlParameter ("@date",reCharge.date), new SqlParameter ("@time",reCharge.time), new SqlParameter ("@UserID",reCharge.UserID), new SqlParameter ("@state",reCharge.state), }; int result = sqlHelper.ExecuteNonQuery(sql, sqlParams, CommandType.Text); //插入 return result; } public int updateStudentCash(Entity.StudentInfo StudentInfo)//更新学生余额 { SQLHelper sqlHelper = new SQLHelper(); SqlParameter[] sqlParams = { new SqlParameter("@CardNo", StudentInfo.CardNo), new SqlParameter ("@cash",StudentInfo.cash )}; string sql = @"UPDATE [Student] SET cash=@cash WHERE cardNo=@cardNo"; int StudentCash = sqlHelper.ExecuteNonQuery(sql, sqlParams, CommandType.Text); return StudentCash; } public DataTable InquiryCardInfo(Entity.StudentInfo StudentInfo) { SQLHelper sqlhelper = new SQLHelper(); SqlParameter[] sqlParams ={ new SqlParameter("@CardNo",StudentInfo.CardNo) }; string sql = "select* from Student where CardNo=@CardNo"; DataTable dt = sqlhelper.ExecuteQuery(sql, sqlParams, CommandType.Text); //int i = dt.Rows.Count; return dt; } } }