校园银行自助系统
这几天由于要交数据库课程设计,就做了一个校园自助银行系统。由于经验不多,系统做的比较简单,没有用三层结构,一些数据库的操作就在用户层实现了。以下是这个系统的所有代码:
软件设计思路
1、 首先建立一个登陆界面,然后做主界面,主界面有以下5个功能:取款,存款,转账,转账,信息查询及历史账单查询,修改密码
2、 在写数据连接代码时,我写了一个SqlHelper类,具体代码如下
1 public class SqlHelper 2 { 3 //连接数据库的字符串 4 static string connStr = @"Data Source=zhao\SQLEXPRESS;Initial Catalog=校园自助银行系统;Integrated Security=True"; 5 //返回一个表 6 public static DataTable ExecuteDataTable(string sql, params SqlParameter[] param) 7 { 8 DataTable dt = new DataTable(); 9 using (SqlConnection conn = new SqlConnection(connStr)) 10 { 11 conn.Open(); 12 using (SqlCommand cmd = new SqlCommand(sql, conn)) 13 14 { 15 if (param != null) 16 { 17 cmd.Parameters.AddRange(param); 18 } 19 SqlDataAdapter sad = new SqlDataAdapter(cmd); 20 sad.Fill(dt); 21 } 22 } 23 return dt; 24 } 25 //执行增删改 26 public static int ExecuteNonQuery(string sql, params SqlParameter[] param) 27 { 28 int result = -1; 29 using (SqlConnection conn = new SqlConnection(connStr)) 30 { 31 conn.Open(); 32 using (SqlCommand cmd = new SqlCommand(sql, conn)) 33 { 34 if (param != null) 35 { 36 cmd.Parameters.AddRange(param); 37 } 38 result = cmd.ExecuteNonQuery(); 39 } 40 } 41 return result; 42 } 43 44 //获取登录用户的信息 45 public static DataRow GetDataRow() 46 { 47 DataTable dt = SqlHelper.ExecuteDataTable("select * from 账户信息 where CardNumber=@cardnumber", new SqlParameter("@cardnumber", UserCardInfo.usercardinfo)); 48 DataRow dr = dt.Rows[0]; 49 return dr; 50 51 } 52 }
在以后的对数据库操作的过程中,都是使用以上类中的方法
3、在做登录界面前,先在主界面使用以下代码,在软件运行时,先弹出登录界面
1 Login login = new Login(); //Login是登录对话框 2 login.ShowDialog();
在进入登录界面时,我先定义一个随机数,用于验证码功能的实现
1 Random r = new Random(); 2 lblYanZheng.Text = r.Next(1000, 9999).ToString(); //验证码的范围是1000到9999
以下代码是具体的登录代码
1 int count = 3; 2 private void btnLogin_Click(object sender, EventArgs e) 3 { 4 //记录银行卡号 5 string cardnumber = txtCardNumber.Text; 6 //记录登录密码 7 string secret = txtCardSecret.Text; 8 //查询数据库中是否有相对应的用户 9 string sqlstr = "select * from 账户信息 where CardNumber=@cardnumber and Secret=@secret"; 10 DataTable dt = SqlHelper.ExecuteDataTable(sqlstr, new SqlParameter("@cardnumber", cardnumber), new SqlParameter("@secret", secret)); 11 //如果查询结果大于0个,则表示有该用户 12 if (dt.Rows.Count > 0) 13 { 14 //如果验证码正确,则允许登录 15 if (txtYanZheng.Text == lblYanZheng.Text) 16 { 17 //用用户信息类的usercardinfo变量保存银行卡号 18 UserCardInfo.usercardinfo = cardnumber; 19 //选中查询结果的第一行 20 DataRow dr = dt.Rows[0]; 21 DataTable dtUser = SqlHelper.ExecuteDataTable("select * from 用户 where CardNumber=@cardnumber", new SqlParameter("@cardnumber", cardnumber)); 22 DataRow drUser = dtUser.Rows[0]; 23 UserCardInfo.name = drUser["Name"].ToString(); 24 //登录成功后,关闭登录对话框 25 this.Close(); 26 } 27 else 28 { 29 //如果验证码,则计数器递减 30 count--; 31 if (count <= 0) 32 { 33 MessageBox.Show("密码输错次数过多,程序即将退出"); 34 System.Environment.Exit(0); 35 } 36 ShowMsg("验证码错误,您还有"+count+"次机会"); 37 return; 38 } 39 } 40 else 41 { 42 //如果用户名或密码错误,则计数器递减 43 if (dt.Rows.Count <=0) 44 { 45 count--; 46 if (count <= 0) 47 { 48 MessageBox.Show("密码输错次数过多,程序即将退出"); 49 System.Environment.Exit(0); 50 } 51 ShowMsg("银行账号或密码错误,您还有" + count + "次机会"); 52 return; 53 } 54 //如果验证码错误 55 else if (txtYanZheng.Text != lblYanZheng.Text) 56 { 57 count--; 58 if (count <= 0) 59 { 60 MessageBox.Show("密码输错次数过多,程序即将退出"); 61 System.Environment.Exit(0); 62 } 63 ShowMsg("验证码错误,您还有" + count + "次机会"); 64 return; 65 } 66 } 67 } 68 69 private void btnCancle_Click(object sender, EventArgs e) 70 { 71 //彻底退出程序 72 System.Environment.Exit(0); 73 } 74 void ShowMsg(string msg) 75 { 76 //显示消息 77 MessageBox.Show(msg); 78 } 79 80 private void Login_Load(object sender, EventArgs e) 81 { 82 //设置将关闭按钮隐藏 83 this.ControlBox = false; 84 }
4、进入主界面后
1 //打开取款对话框 2 private void lblQuKuan_Click(object sender, EventArgs e) 3 { 4 QuKuan qck = new QuKuan(); 5 qck.ShowDialog(); 6 } 7 //打开存款对话框 8 private void lblCunKuan_Click(object sender, EventArgs e) 9 { 10 CunKuan qck = new CunKuan(); 11 qck.ShowDialog(); 12 } 13 //打开转账对话框 14 private void lblZhuanZhang_Click(object sender, EventArgs e) 15 { 16 ZhuanZhang zz = new ZhuanZhang(); 17 zz.ShowDialog(); 18 } 19 //打开查询个人信息对话框 20 private void lblChaXun_Click(object sender, EventArgs e) 21 { 22 ChaXun cx = new ChaXun(); 23 cx.ShowDialog(); 24 } 25 //打开修改密码对话框 26 private void lblXiuGai_Click(object sender, EventArgs e) 27 { 28 ChangeSecret cs = new ChangeSecret(); 29 cs.ShowDialog(); 30 } 31 32 //欢迎词做跑龙灯运动 33 private void lbltimer_Tick(object sender, EventArgs e) 34 { 35 lblWelcome.Text = lblWelcome.Text.Substring(1) + lblWelcome.Text.First(); 36 } 37 38 private void 切换账户ToolStripMenuItem_Click(object sender, EventArgs e) 39 { 40 //打开登录对话框 41 Login login = new Login(); 42 login.ShowDialog(); 43 44 DataTable dt = SqlHelper.ExecuteDataTable("select * from 用户 where CardNumber=@cardnumber", new SqlParameter("@cardnumber", UserCardInfo.usercardinfo)); 45 46 //更新主界面用户名称 47 DataRow dr = dt.Rows[0]; 48 lblUserInfo.Text = dr["Name"].ToString(); 49 } 50 51 private void 安全退出ToolStripMenuItem_Click(object sender, EventArgs e) 52 { 53 //退出应用程序 54 Application.Exit(); 55 } 56 57 private void Form1_Load(object sender, EventArgs e) 58 { 59 //显示登录用户的名称 60 lblUserInfo.Text = ""; 61 DataTable dt = SqlHelper.ExecuteDataTable("select * from 用户 where CardNumber=@cardnumber",
new SqlParameter("@cardnumber",UserCardInfo.usercardinfo)); 62 DataRow dr = dt.Rows[0]; 63 lblUserInfo.Text = dr["Name"].ToString(); 64 }
5、在进入主界面后,我定义了一个类UserCardInfo,里面定义了几个字段,用记录用户信息
1 static class UserCardInfo 2 { 3 //保存用户登录的卡号 4 public static string usercardinfo; 5 //保存交易时间 6 public static string time; 7 //保存交易类型 8 public static string type; 9 //保存交易金额 10 public static string money; 11 //保存账户余额 12 public static string remain; 13 //保存用户姓名 14 public static string name; 15 }
6、在做存款取款转账功能前,我先写了CunQunKuan类,用于定义存取款转账方法,具体代码如下:
1 static class CunQuKuan 2 { 3 //如果b为true,则为取款,否则为转账 4 public static bool QuKuan(decimal money,bool b) 5 { 6 DataRow dr = SqlHelper.GetDataRow(); 7 //先获得当前用户的余额 8 decimal moneyRemain = decimal.Parse(dr["Money"].ToString()); 9 //如果取款或转账的金额大于账户余额,则失败 10 if (money > moneyRemain) 11 { 12 MessageBox.Show("余额不足,取款失败!"); 13 return false; 14 } 15 else 16 { 17 //如果成功,则将余额减去所取的金额 18 moneyRemain -= money; 19 //更新数据库,将该用户的账户余额改为取款后的余额 20 SqlHelper.ExecuteNonQuery("update 账户信息 set Money=@moneyRemain where CardNumber=@cardnumber ", new SqlParameter("@moneyRemain", moneyRemain),
new SqlParameter("@cardnumber", dr["CardNumber"].ToString())); 21 if (b == true) 22 { 23 MessageBox.Show("取款成功!"); 24 } 25 else 26 { 27 MessageBox.Show("转账成功!"); 28 } 29 return true; 30 } 31 } 32 33 //存款的方法 34 public static void CunKuan(decimal money) 35 { 36 DataRow dr = SqlHelper.GetDataRow(); 37 decimal moneyRemain = decimal.Parse(dr["Money"].ToString()); 38 moneyRemain += money; 39 SqlHelper.ExecuteNonQuery("update 账户信息 set Money=@moneyRemain where CardNumber=@cardnumber", new SqlParameter("@moneyRemain", moneyRemain),
new SqlParameter("@cardnumber", dr["CardNumber"].ToString())); 40 MessageBox.Show("存款成功!"); 41 } 42 }
7、在做好以上工作以后,我做了取款功能,代码如下:
1 private void btnOk_Click(object sender, EventArgs e) 2 { 3 decimal money = decimal.Parse(txtMoney.Text); 4 //执行取款操作,并获得返回值,如果返回true,则打印凭证 5 bool b= CunQuKuan.QuKuan(money, true); 6 if (b) 7 { 8 DialogResult result = MessageBox.Show("是否打印凭条", "打印凭条", MessageBoxButtons.YesNo); 9 DataRow dr = SqlHelper.GetDataRow(); 10 //如果点是,则打印凭证 11 if (result == DialogResult.Yes) 12 { 13 //将标签的文本改为取款信息 14 lblprint.Text = string.Format("卡号:{0}\t\n姓名:{1}\t\n时间:{2}\t\n取款:{3}", dr["CardNumber"].ToString(), UserCardInfo.name, DateTime.Now, money.ToString()); 15 //将标签状态改为可视 16 lblprint.Visible = true; 17 } 18 //向交易信息中插入取款记录 19 AddJYInfo.MyMethod(dr,money,"取款"); 20 } 21 } 22 23 private void btnBack_Click(object sender, EventArgs e) 24 { 25 //关闭取款对话框 26 this.Close(); 27 } 28 29 //当窗口加载的时候,显示账户余额 30 private void QuKuan_Load(object sender, EventArgs e) 31 { 32 DataRow dr = SqlHelper.GetDataRow(); 33 lblMoneyRemain.Text += dr["Money"].ToString(); 34 }
8、以下是存款功能代码:
1 private void btnOk_Click(object sender, EventArgs e) 2 { 3 //获得输入的存款金额 4 decimal money = decimal.Parse(txtMoney.Text); 5 //执行存款程序 6 CunQuKuan.CunKuan(money); 7 //返回用户信息所在行 8 DataRow dr=SqlHelper.GetDataRow(); 9 //想交易信息中添加存款记录 10 AddJYInfo.MyMethod(dr, money, "存款"); 11 } 12 13 private void btnBack_Click(object sender, EventArgs e) 14 { 15 //关闭存款对话框 16 this.Close(); 17 }
9、以下是转账代码:
1 private void btnOk_Click(object sender, EventArgs e) 2 { 3 decimal money = decimal.Parse(txtMoney.Text); 4 string receiver = txtReceiver.Text; 5 //获取转账用户的记录 6 DataRow dr=SqlHelper.GetDataRow(); 7 //如果转账的金额大于账户余额,则禁止转账 8 if (money >decimal.Parse(dr["Money"].ToString())) 9 { 10 MessageBox.Show("余额不足,转账失败!"); 11 return; 12 } 13 else 14 { 15 DataTable dt = SqlHelper.ExecuteDataTable("select * from 账户信息 where CardNumber=@receiver", new SqlParameter("@receiver", receiver)); 16 //如果数据库中存在接收转账的用户 17 if (dt.Rows.Count > 0) 18 { 19 DataRow drReceiver = dt.Rows[0]; 20 //查询接收转账的用户的金额 21 decimal moneyReceiver = decimal.Parse(drReceiver["Money"].ToString()); 22 //接收者的金额加上转的金额 23 moneyReceiver += money; 24 //更新接收转账的用户的金额 25 SqlHelper.ExecuteNonQuery("update 账户信息 set Money=@moneyReceiver where CardNumber=@receiver", new SqlParameter("@moneyReceiver", moneyReceiver),
new SqlParameter("@receiver", receiver)); 26 //执行取款方法,false表示弹出的消息框为转账成功 27 CunQuKuan.QuKuan(money,false); 28 dr = SqlHelper.GetDataRow(); 29 //向交易信息中添加转账记录 30 AddJYInfo.MyMethod(dr, money, "转账"); 31 } 32 else 33 { 34 MessageBox.Show("该用户不存在,请核对账户是否正确"); 35 return; 36 } 37 } 38 39 } 40 41 42 private void btnBack_Click(object sender, EventArgs e) 43 { 44 //关闭转账对话框 45 this.Close(); 46 }
10、同时,为了记录用户存款取款转账记录,我定义了AddJYInfo类,代码如下:
1 class AddJYInfo 2 { 3 //向交易信息表中添加交易信息记录 4 public static void MyMethod(DataRow dr, Decimal money, string type) 5 { 6 //银行卡号 7 UserCardInfo.usercardinfo = dr["CardNumber"].ToString(); 8 //交易金额 9 UserCardInfo.money = money.ToString(); 10 //交易时间 11 UserCardInfo.time = DateTime.Now.ToString(); 12 //交易类型 13 UserCardInfo.type = type; 14 //交易后账户余额 15 UserCardInfo.remain = dr["Money"].ToString(); 16 //向数据库中插入交易记录 17 string sql = "insert into 交易记录 (CardNumber,Time,Money,Type,Remain) Values(@usercardinfo, @time,@money,@type,@remain)"; 18 SqlHelper.ExecuteNonQuery(sql, new SqlParameter("@usercardinfo", UserCardInfo.usercardinfo),
new SqlParameter("@time", UserCardInfo.time),
new SqlParameter("@money", UserCardInfo.money),
new SqlParameter("@type", UserCardInfo.type),
new SqlParameter("@remain", UserCardInfo.remain)); 19 } 20 }
11、接下来是信息查询及历史交易记录查询功能,在做历史交易记录查询功能时,我添加了类AddData,定义了一个根据所选的年份和月份动态添加每个月的天数,代码如下:
1 class AddData 2 { 3 public static List<int> AddDay(int year, int month) 4 { 5 List<int> list = new List<int>(); 6 if (month == 1 || month == 3 || month == 5 || month == 7 || month == 9 || month == 11) 7 { 8 list.Clear(); 9 for (int i = 1; i <= 31; i++) 10 { 11 list.Add(i); 12 } 13 } 14 else if (month == 4 || month == 6 || month == 8 || month == 10 || month == 12) 15 { 16 list.Clear(); 17 for (int i = 1; i <= 30; i++) 18 { 19 list.Add(i); 20 } 21 } 22 else 23 { 24 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) 25 { 26 list.Clear(); 27 for (int i = 1; i <= 29; i++) 28 { 29 list.Add(i); 30 } 31 } 32 else 33 { 34 list.Clear(); 35 for (int i = 1; i <= 28; i++) 36 { 37 list.Add(i); 38 } 39 } 40 } 41 return list; 42 } 43 }
12、以下是信息查询及历史交易记录查询代码:
1 private void ChaXun_Load(object sender, EventArgs e) 2 { 3 //获取该用户信息 4 DataRow dr = SqlHelper.GetDataRow(); 5 //获得银行卡号 6 string cardnumber = dr["CardNumber"].ToString(); 7 //将表格控件的数据源绑定 8 dataGridView.DataSource = SqlHelper.ExecuteDataTable("select * from 用户 where CardNumber=@cardnumber", new SqlParameter("@cardnumber", cardnumber)); 9 //选择起始年份的默认项为下标为0的项 10 cbStartYear.SelectedIndex = 0; 11 //选择截止年份的默认项为下标为0的项 12 cbEndYear.SelectedIndex = 0; 13 //选择起始月份的默认项为下标为5的项 14 cbStartMonth.SelectedIndex = 5; 15 //选择截止月份的默认项为下标为5的项 16 cbEndMonth.SelectedIndex = 5; 17 } 18 19 private void btnOk_Click(object sender, EventArgs e) 20 { 21 //关闭查询对话框 22 this.Close(); 23 } 24 25 private void btnQuery_Click(object sender, EventArgs e) 26 { 27 28 int startyear = int.Parse(cbStartYear.SelectedItem.ToString()); 29 int startmonth = int.Parse(cbStartMonth.SelectedItem.ToString()); 30 31 int endyear = int.Parse(cbEndYear.SelectedItem.ToString()); 32 int endmonth = int.Parse(cbEndMonth.SelectedItem.ToString()); 33 34 try 35 { 36 int startday = int.Parse(cbStartDay.SelectedItem.ToString()); 37 int endday = int.Parse(cbEndDay.SelectedItem.ToString()); 38 39 if (startyear > endyear || (startyear <= endyear && startmonth > endmonth) || (startyear <= endyear && startmonth <= endmonth && startday > endday)) 40 { 41 MessageBox.Show("请输入正确的查询日期范围"); 42 return; 43 } 44 else 45 { 46 DataTable dt = SqlHelper.ExecuteDataTable("select * from 交易记录 where Year(Time)>=@startyear and Year(Time)<=@endyear and Month(Time)>=@startmonth and Month(Time)<=@endmonth and Day(Time)>=@startday and Day(Time)<=@endday and CardNumber=@cardnumber", new SqlParameter("@startyear", startyear),
new SqlParameter("@endyear", endyear),
new SqlParameter("@startmonth", startmonth),
new SqlParameter("@endmonth", endmonth),
new SqlParameter("@startday", startday),
new SqlParameter("@endday", endday),
new SqlParameter("@cardnumber", UserCardInfo.usercardinfo)); 47 if (dt.Rows.Count > 0) 48 { 49 tableHistory.DataSource = dt; 50 } 51 else 52 { 53 tableHistory.DataSource = null; 54 MessageBox.Show("sorry,未查询到相关数据"); 55 } 56 } 57 } 58 catch (Exception) 59 { 60 MessageBox.Show("请选择查询日期"); 61 } 62 63 } 64 //当起始月份的选择项索引发生变化时发生的事件 65 private void cbStartMonth_SelectedIndexChanged(object sender, EventArgs e) 66 { 67 int month = int.Parse(cbStartMonth.SelectedItem.ToString()); 68 int year = int.Parse(cbStartYear.SelectedItem.ToString()); 69 //先清空之前已添加的天数 70 cbStartDay.Items.Clear(); 71 foreach (int item in AddData.AddDay(year, month)) 72 { 73 //向起始天下拉框中添加天数 74 cbStartDay.Items.Add(item); 75 } 76 77 } 78 //当截止月份的选择项索引发生变化时发生的事件 79 private void cbEndMonth_SelectedIndexChanged(object sender, EventArgs e) 80 { 81 int month = int.Parse(cbEndMonth.SelectedItem.ToString()); 82 int year = int.Parse(cbEndYear.SelectedItem.ToString()); 83 cbEndDay.Items.Clear(); 84 foreach (int item in AddData.AddDay(year, month)) 85 { 86 cbEndDay.Items.Add(item); 87 } 88 89 }
13、最后是修改密码功能,具体代码如下:
1 //设置允许改密码错误的次数 2 int count = 3; 3 private void btnOk_Click(object sender, EventArgs e) 4 { 5 //或旧密码 6 string oldpwd = txtOldPwd.Text; 7 //获得输入的新密码 8 string newpwd = txtNewPwd.Text; 9 //获得确认的新密码 10 string newpwd2 = txtNewPwd2.Text; 11 //查询该用户信息 12 DataRow dr = SqlHelper.GetDataRow(); 13 //如果输入的旧密码正确和两次输入的新密码都一致,则修改密码成功 14 if (oldpwd == dr["secret"].ToString() && newpwd == newpwd2) 15 { 16 SqlHelper.ExecuteNonQuery("update 账户信息 set Secret=@secret where CardNumber=@cardnumber", new SqlParameter("@secret", newpwd),
new SqlParameter("@cardnumber", dr["CardNumber"].ToString())); 17 MessageBox.Show("恭喜您,密码修改成功!"); 18 } 19 //如果旧密码错误,则要求重新输入旧密码,如果输错三次,则退出修改密码对话框 20 else if (oldpwd != dr["secret"].ToString()) 21 { 22 MessageBox.Show("原始密码不正确,您还剩" + --count + "次机会"); 23 if (count <= 0) 24 { 25 this.Close(); 26 } 27 return; 28 } 29 //如果两次输入的新密码不一致 30 else if (newpwd != newpwd2) 31 { 32 MessageBox.Show("亲,您的俩次密码不一致哦!再检查一下吧"); 33 return; 34 } 35 36 } 37 38 private void btnCancle_Click(object sender, EventArgs e) 39 { 40 //关闭修改密码对话框 41 this.Close(); 42 }
最后总结一下做这个系统之后的感受吧,以前学习的时候,不知道为什么别人要定义这个类,为什么这个方法的返回值要是这个类型,其实大可不必纠结这些问题,在自己做的时候,就会发现根据项目的需要,自然而然的需要做这些事情。通过这次实践,更加加深了我对ADO.Net的理解,在后面的学习过程中,多实践多练习才是快速掌握知识的正确途径,没有一定代码的积累,是很难成为真正的高手。