📄 bank.cs
字号:
using System;
using System.EnterpriseServices;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Collections;
[assembly:ApplicationAccessControl(
AccessChecksLevel=AccessChecksLevelOption.ApplicationComponent,
Authentication=AuthenticationOption.Call,
ImpersonationLevel=ImpersonationLevelOption.Delegate
)]
[assembly:ApplicationActivation(ActivationOption.Server)]
namespace Bank
{
//定义所需的所有操作
public interface BankOperation
{
//生成新的帐号
bool CreateAccount(string AccountNo,string CustomerID,string Password,decimal Balance);
//生成新的卡号
bool CreateCard(string CardNo,string AccountNo,string Password);
//生成新的客户
bool CreateCustomer(string CustomerID,string Name,string Gender,DateTime Birthday,string Address,string Phone);
//删除帐号
void DeleteAccount(string AccountNo);
//删除卡号
void DeleteCard(string CardNo);
//删除客户
void DeleteCustomer(string CustomerID);
//利用帐号存款
bool DepositByAccountNo(string AccountNo,decimal amount);
//利用卡号存款
bool DepositByCardNo(string CardNo,decimal amount);
//获取所有的帐号
ArrayList GetAllAccounts();
//获取指定客户的所有帐号
ArrayList GetAllAccountsByCustomerID(string CustomerID);
//获取所有的卡号
ArrayList GetAllCards();
//获取指定帐号的所有卡号
ArrayList GetAllCardsByAccountNo(string AccountNo);
//获取所有的客户
ArrayList GetAllCustomers();
//利用帐号进行查询
decimal QueryByAccountNo(string AccountNo);
//利用卡号进行查询
decimal QueryByCardNo(string CardNo);
//利用帐号进行转帐
bool TransferByAccountNo(string FromAccountNo,string ToAccountNo,decimal amount);
//利用卡号进行转帐
bool TransferByCardNo(string FromCardNo,string ToCardNo,decimal amount);
//更改帐号口令
bool UpdateAccountPassword(string AccountNo,string Pwd);
//更改卡号口令
bool UpdateCardPassword(string CardNo,string Pwd);
//验证帐号
bool VerifyAccountNo(string AccountNo,string Pwd);
//验证卡号
bool VerifyCardNo(string CardNo,string Pwd);
//利用帐号进行取款
bool WithdrawByAccountNo(string AccountNo,decimal amount);
//利用卡号进行取款
bool WithdrawByCardNo(string CardNo,decimal amount);
}
//支持事务
[Transaction(TransactionOption.Supported)]
//使用对象池
[ObjectPooling(true,10,50,CreationTimeout=30000)]
//使用及时激活(JITA)
[JustInTimeActivation]
//定义构造字符串
[ConstructionEnabled(
Default="data source=localhost;initial catalog=bank;user id=sa;password=sa;"
)]
//定义安全角色
[SecurityRole("bank",true)]
[ComponentAccessControl]
public class Bank:ServicedComponent,BankOperation
{
private string connString;
public Bank()
{
}
//重置Construct()方法
protected override void Construct(string s)
{
base.Construct (s);
connString=s;
}
//获取数据库连接
private SqlConnection GetConnection()
{
SqlConnection conn=new SqlConnection();
conn.ConnectionString=connString;
return conn;
}
//利用给的数据库连接和查询命令字符串获取SqlDataReader
private SqlDataReader GetDataReader(string cmdString,SqlConnection conn)
{
SqlCommand comm=new SqlCommand();
comm.CommandText=cmdString;
comm.Connection=conn;
SqlDataReader reader=comm.ExecuteReader();
return reader;
}
//更新数据库
private void Update(string cmdString,SqlConnection conn)
{
SqlCommand comm=new SqlCommand();
comm.CommandText=cmdString;
comm.Connection=conn;
comm.ExecuteNonQuery();
}
//由卡号查询帐号,成功,返回帐号;否则,返回"error"
private string QueryAccountNo(string CardNo)
{
string result;
SqlConnection conn=GetConnection();
conn.Open();
string cmdString="select AccountNo from Card where CardNo='"+
CardNo+"'";
SqlDataReader reader=GetDataReader(cmdString,conn);
if(reader.Read())
{
result=reader.GetString(0);
}
else
{
result="error";
}
reader.Close();
conn.Close();
return result;
}
//判断帐号是否存在。存在,返回真;否则,返回假
private bool AccountNoExists(string AccountNo)
{
bool result;
SqlConnection conn=GetConnection();
conn.Open();
string cmdString="select * from Account where AccountNo='"+
AccountNo+"'";
SqlDataReader reader=GetDataReader(cmdString,conn);
if(reader.Read())
result=true;
else
result=false;
reader.Close();
conn.Close();
return result;
}
//利用帐号进行查询,帐号不存在,返回-1
public decimal QueryByAccountNo(string AccountNo)
{
string cmdString="select balance from Account where AccountNo='"+
AccountNo+"'";
decimal balance=-1;
SqlConnection conn=GetConnection();
conn.Open();
SqlDataReader reader=GetDataReader(cmdString,conn);
if(reader.Read())
{
balance= reader.GetDecimal(0);
}
else
{
balance=-1;
}
reader.Close();
conn.Close();
return balance;
}
//利用卡号进行查询,卡号不存在,返回-1
public decimal QueryByCardNo(string CardNo)
{
decimal balance=-1;
string AccountNo;
AccountNo=QueryAccountNo(CardNo);
if(AccountNo=="error")
{
balance=-1;
}
else
{
balance=QueryByAccountNo(AccountNo);
}
return balance;
}
//利用帐号取款,余额不够时,返回假;成功,返回真
public bool WithdrawByAccountNo(string AccountNo,decimal ammount)
{
bool result;
decimal balance;
string cmdString="select balance from Account where AccountNo='"+
AccountNo+"'";
SqlConnection conn=GetConnection();
conn.Open();
SqlDataReader reader=GetDataReader(cmdString,conn);
reader.Read();
balance=reader.GetDecimal(0);
reader.Close();
if(balance<ammount)
result=false;
else
{
string UpdateString="update Account set balance=balance-"+
ammount+" where AccountNo='"+AccountNo+"'";
Update(UpdateString,conn);
result=true;
}
conn.Close();
return result;
}
//利用卡号取款,余额不够时,返回假;成功,返回真
public bool WithdrawByCardNo(string CardNo,decimal ammount)
{
bool result;
string AccountNo;
AccountNo=QueryAccountNo(CardNo);
if(AccountNo=="error")
{
result=false;
}
else
{
result=WithdrawByAccountNo(AccountNo,ammount);
}
return result;
}
//利用帐号进行转帐
public bool TransferByAccountNo(string FromAccountNo,string ToAccountNo,decimal amount)
{
bool result=false;
decimal balance;
if(!AccountNoExists(FromAccountNo)||!AccountNoExists(ToAccountNo))
return false;//任何一个帐号不存在,操作失败
SqlConnection conn=GetConnection();
conn.Open();
string cmdString="select balance from Account where AccountNo='"+
FromAccountNo+"'";
SqlDataReader reader=GetDataReader(cmdString,conn);
reader.Read();
balance=reader.GetDecimal(0);
reader.Close();
if(balance<amount)//余额不走,操作失败
result=false;
else
{
cmdString="update Account set balance=balance-"+amount+" where AccountNo='"+
FromAccountNo+"'";
Update(cmdString,conn);
cmdString="update Account set balance=balance+"+amount+" where AccountNo='"+
ToAccountNo+"'";
Update(cmdString,conn);
result=true;
}
return result;
}
//利用卡号进行转帐,成功,返回真;否则,返回假
public bool TransferByCardNo(string FromCardNo,string ToCardNo,decimal amount)
{
bool result=false;
string FromAccountNo;
string ToAccountNo;
FromAccountNo=QueryAccountNo(FromCardNo);
ToAccountNo=QueryAccountNo(ToCardNo);
if(FromAccountNo=="error"||ToAccountNo=="error")
result=false;
else
result=TransferByAccountNo(FromAccountNo,ToAccountNo,amount);
return result;
}
//利用帐号进行存款,成功,返回真;否则,返回假
public bool DepositByAccountNo(string AccountNo,decimal amount)
{
if(!AccountNoExists(AccountNo))
return false;
SqlConnection conn=GetConnection();
conn.Open();
string cmdString="update Account set balance=balance+"+amount+
"where AccountNo='"+AccountNo+"'";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -