C#模擬銀行轉賬系統
類庫:BankModel
建立類:Bank
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BankModel
{
public class Bank
{
private string A_id; //賬號
public string A_id1
{
get { return A_id; }
set { A_id = value; }
}
private string A_pwd; //密碼
public string A_pwd1
{
get { return A_pwd; }
set { A_pwd = value; }
}
private string A_host; //賬戶人姓名
public string A_host1
{
get { return A_host; }
set { A_host = value; }
}
private string A_state; //1 可用 2掛失 3凍結
public string A_state1
{
get { return A_state; }
set { A_state = value; }
}
private string A_left; //賬戶內金額
public string A_left1
{
get { return A_left; }
set { A_left = value; }
}
}
}
類庫:BankDAL 引用BankModel.dll檔案
類:DALBank
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace BankDAL
{
public class DALBank
{
public static int Bankpro(string proName, SqlParameter[] pars) {
//Sql指令碼
return DBHelper.CreateInstance().ExecPro(proName, pars);
}
}
}
類:DBHelper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace BankDAL
{
internal class DBHelper
{
private SqlConnection con=null;
private SqlCommand cmd = null;
private static DBHelper dbh = null;
private DBHelper() {
con = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnectionBank"].ConnectionString);
}
public static DBHelper CreateInstance()
{
if (dbh==null)
{
dbh = new DBHelper();
}
return dbh;
}
/// <summary>
/// 執行T-SQL儲存過程
/// </summary>
/// <param name="proName">儲存過程名</param>
/// <param name="pars">引數列表</param>
/// <returns></returns>
public int ExecPro(string proName, SqlParameter[] pars)
{
//命名物件
cmd = new SqlCommand();
//連線
cmd.Connection = con;
//型別
cmd.CommandType = CommandType.StoredProcedure;
//過程名
cmd.CommandText = proName;
//引數
cmd.Parameters.AddRange(pars);
try
{
con.Open();
cmd.ExecuteNonQuery();
//返回首引數值
return Convert.ToInt32(pars[0].Value);
}
catch (Exception)
{
//系統異常
return -1;
}
finally {
con.Close();
}
}
/// <summary>
/// 執行Sql指令碼
/// </summary>
/// <param name="strsql"></param>
/// <returns></returns>
public int EditSql(string strsql) {
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnectionBank"].ConnectionString))
{
cmd = new SqlCommand(strsql, conn);
try
{
con.Open();
//返回受影響行數
return cmd.ExecuteNonQuery();
}
catch (Exception)
{
//系統異常
return -1;
}
}
}
}
}
建立類庫:BankBLL 引用:BankDAL.dll檔案
類:BLLBank
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BankDAL;
namespace BankBLL
{
public class BLLBank
{
public static int BankLogin(string id, string pwd) {
//配置引數s
SqlParameter[] pars = new SqlParameter[3];
pars[0] = new SqlParameter("@flag", SqlDbType.Int);
pars[0] .Direction=ParameterDirection.Output;
//pars[0].Value = 0;
pars[1] = new SqlParameter("@id", SqlDbType.Char, 9);
pars[1].Value = id;
pars[2] = new SqlParameter("@pwd",SqlDbType.Char,6);
pars[2].Value = pwd;
return DALBank.Bankpro("pro_AccountLogin", pars);
}
public static int BankTran(string fromId, string toId,double money) {
SqlParameter[] pars = new SqlParameter[4];
pars[0] = new SqlParameter("@flag", SqlDbType.Int);
pars[0].Direction = ParameterDirection.Output;
pars[1] = new SqlParameter("@fromId", SqlDbType.Char, 9);
pars[1].Value = fromId;
pars[2] = new SqlParameter("@toId", SqlDbType.Char, 9);
pars[2].Value = toId;
pars[3] = new SqlParameter("@money", SqlDbType.Money);
pars[3].Value = money;
return DALBank.Bankpro("pro_AccountTranTest",pars);
}
}
}
類庫:BankUIL 引用:BankBLL.dll檔案
窗體:FrmLogin
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BankBLL;
namespace BankUIL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 登入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLogin_Click(object sender, EventArgs e)
{
string id = this.mktbID.Text;
string pwd = this.mktbPwd.Text;
//正則驗證
int Loginresult = BLLBank.BankLogin(id, pwd);
switch (Loginresult)
{
case -1:
{
MessageBox.Show("系統異常!");
break;
}
case 0:
{
this.grbLogin.Enabled = false;
this.grbTran.Enabled = true;
break;
}
case 1:
{
MessageBox.Show("使用者名稱或密碼錯誤!");
break;
}
case 2:
{
MessageBox.Show("賬號被鎖定!");
break;
}
}
}
/// <summary>
/// 轉賬
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnTran_Click(object sender, EventArgs e)
{
string fromId = this.mktbID.Text;
string toId = this.mktbtoId.Text;
double money = (double)this.nudTranMoney.Value;
int resultTran = BLLBank.BankTran(fromId,toId,money);
switch (resultTran)
{
case -1:
{
MessageBox.Show("系統異常!");
break;
}
case 0:
{
MessageBox.Show("轉賬成功!");
break;
}
case 1:
{
MessageBox.Show("同一賬戶不能轉賬!");
break;
}
case 2:
{
MessageBox.Show("轉入賬號不存在!");
break;
}
case 3:
{
MessageBox.Show("轉出賬戶餘額不足!");
break;
}
case 4:
{
MessageBox.Show("轉入賬號不能使用!");
break;
}
case 5:
{
MessageBox.Show("資料庫異常!");
break;
}
}
}
}
}
配置檔案:App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="SqlConnectionBank" connectionString="server=192.168.20.48;database=BankDB;uid=admin;pwd=master"/>
</connectionStrings>
</configuration>