- 介绍银行模拟系统的作用和意义。
- 讲解如何使用C#连接SQL数据库。
- 介绍银行模拟系统的基本功能,例如账户开户、存款取款、转账等。
- 提供代码示例,让读者更好地理解和实践。
- 总结整个模拟系统的设计思路和开发过程,以及可能的后续扩展和改进。
首先创建4个类
可以自己创建
首先是第一个类:Connom.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApp8
{
internal class Connom
{
string str = "Data Source=.;Initial Catalog=Myba;User ID=sa;Password=123";
SqlConnection conn;
public SqlConnection Connection
{
get
{
if (conn == null)
{
conn = new SqlConnection(str);
}
return conn;
}
}
public void sqlOpen()
{
if (Connection.State == ConnectionState.Closed)
{
Connection.Open();
}
else if (Connection.State == ConnectionState.Broken)
{
Connection.Close();
Connection.Open();
}
}
public void sqlclose()
{
if (Connection.State == ConnectionState.Open || Connection.State == ConnectionState.Broken)
{
Connection.Close();
}
}
}
}
这个类主要是连接数据库方便调用 如有看不到请联系
然后是Pand.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp8
{
internal class Pand
{
sql sql_1= new sql();
public void directory() {
Console.WriteLine("输入姓名:");
string username = Console.ReadLine();
if (string.IsNullOrEmpty(username))
{
Console.WriteLine("用户名不能为空");
directory();
return;
}
Console.WriteLine("输入密码:");
string password = Console.ReadLine();
bool isAmaonga = sql_1._VerifyLogin(username, password);
if (isAmaonga)
{
Console.WriteLine("登录成功");
director();
}
else {
Console.WriteLine("账号和密码错误");
}
} //验证密码登录已经完成
public void director() {
Console.WriteLine("------------------------");
Console.WriteLine("1.查看余额");
Console.WriteLine("2.修改密码");
Console.WriteLine("3.存款");
Console.WriteLine("4.取款");
Console.WriteLine("5.转款");
Console.WriteLine("6.退出");
Console.WriteLine("------------------------");
Console.WriteLine("请您选择您要办理的业务:");
int aps = int.Parse(Console.ReadLine());
switch (aps) {
case 1:
sql_1._ModifyBalance();
break;
case 2:
Console.WriteLine("输入原始密码:");
string oldpasd= Console.ReadLine();
sql_1._ChangePassword(oldpasd);
break;
case 3:
Console.WriteLine("请输入存款金额:");
string newpasd= Console.ReadLine();
sql_1._UpdateBalance(newpasd);
break;
case 4:
Console.WriteLine("请输入取款款金额:");
int newpasd1 = int.Parse(Console.ReadLine());
sql_1._UpdateBalanceQu(newpasd1);
break;
case 5:
while (1==1)
{
Console.WriteLine("请输入你要转款的的名字:");
string name = Console.ReadLine();
Console.WriteLine("请输入转款的金额:");
decimal moeny = int.Parse(Console.ReadLine());
if (moeny > 0)
{
sql_1._ZhuanK(name, moeny);
break;
}
else
{
Console.WriteLine("转款金额必须大于0元");
continue;
}
}
break;
case 6:
sql_1.ajg = string.Empty;
directory();
break;
}
}
}
}
简单一个银行管理目录
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Xml.Linq;
using System.Windows.Input;
using System.Security.Policy;
namespace ConsoleApp8
{
internal class sql
{
Connom Connom = new Connom();
public string ajg = string.Empty;
public bool _VerifyLogin(string name,string pad) {
try
{
Connom.sqlOpen();
using (SqlCommand Comm = new SqlCommand(string.Format("select count(*) from GuanL\r\nwhere userName='{0}' and userpassword ='{1}'", name, pad), Connom.Connection)) {
int conut = Convert.ToInt32(Comm.ExecuteScalar());
ajg = name;
return conut > 0;
}
}
catch (Exception ex)
{
Console.WriteLine("错误",ex.Message);
throw;
}
finally {
Connom.sqlclose();
}
} //sql验证登录
public void _ChangePassword(string pasd) {
try
{
Connom.sqlOpen();
using (SqlCommand Comm = new SqlCommand(string.Format("select count(*) from GuanL\r\nwhere userpassword ='{0}'", pasd), Connom.Connection))
{
int conut = Convert.ToInt32(Comm.ExecuteScalar()); //判断两次密码是否一致
if (conut > 0)
{
Console.WriteLine("输入新的密码:");
string newPassword = Console.ReadLine();
// 更新数据库中的密码
string updateQuery = string.Format("UPDATE GuanL SET userpassword = '{0}' WHERE Username = '{1}'", newPassword,ajg);
using (SqlCommand updatecommm = new SqlCommand(updateQuery, Connom.Connection))
{
int a = updatecommm.ExecuteNonQuery();
if (a > 0)
{
Console.WriteLine("密码更新成功!");
}
else
{
Console.WriteLine("密码更新失败!");
}
}
}
else
{
Console.WriteLine("原始密码不正确!");
}
}
}
catch (Exception ex)
{
Console.WriteLine("错误", ex.Message);
throw;
}
finally
{
Connom.sqlclose();
}
} //修改密码
public void _ModifyBalance() {
try
{
Connom.sqlOpen();
SqlCommand Comm = new SqlCommand(string.Format("select * from manage\r\nwhere Uname='{0}'", ajg), Connom.Connection);
SqlDataReader sql = Comm.ExecuteReader();
while (sql.Read())
{
Console.WriteLine("{0},{1},{2},{3}", sql["KaHao"], sql["Uname"], sql["Sex"], sql["CSRQ"], sql["KHRQ"], sql["KHJE"], sql["address"], sql["reportLoss"]);
}
}
catch (Exception ex)
{
Console.WriteLine("错误", ex.Message);
throw;
}
finally
{
Connom.sqlclose();
}
} //查看信息
public void _UpdateBalance(string moeme)
{
try
{
Connom.sqlOpen();
SqlCommand Comm = new SqlCommand(string.Format("UPDATE manage\r\nSET KHJE = KHJE + {0}\r\nWHERE Uname = '{1}'", moeme,ajg), Connom.Connection);
int rowsAffected = Comm.ExecuteNonQuery();
if (rowsAffected > 0)
{
Console.WriteLine("存款金额已经更新");
}
else
{
Console.WriteLine("存款失败");
}
}
catch (Exception ex)
{
Console.WriteLine("错误", ex.Message);
throw;
}
finally
{
Connom.sqlclose();
}
} //存款
public void _UpdateBalanceQu(int moeme)
{
if (_PanDJe(moeme))
{
try
{
Connom.sqlOpen();
SqlCommand Comm = new SqlCommand(string.Format("UPDATE manage\r\nSET KHJE = KHJE - {0}\r\nWHERE Uname = '{1}'", moeme, ajg), Connom.Connection);
int rowsAffected = Comm.ExecuteNonQuery();
if (rowsAffected > 0)
{
Console.WriteLine("取款金额已经更新");
}
else
{
Console.WriteLine("取款失败");
}
}
catch (Exception ex)
{
Console.WriteLine("错误", ex.Message);
throw;
}
finally
{
Connom.sqlclose();
}
}
else
{
Console.WriteLine("你没有那么钱");
}
} //存款
public bool _PanDJe(int money) {
try
{
Connom.sqlOpen();
SqlCommand Comm = new SqlCommand(string.Format("select KHJE from manage\r\nwhere Uname='{0}'",ajg), Connom.Connection);
SqlDataReader red = Comm.ExecuteReader();
red.Read();
if ((int)red["KHJE"] > money) {
return true;
}
return false;
}
catch (Exception ex)
{
Console.WriteLine("错误", ex.Message);
throw;
}
finally
{
Connom.sqlclose();
}
}
public void _ZhuanK(string name, decimal money)
{
try
{
转款减钱
Connom.sqlOpen();
SqlCommand Comm = new SqlCommand(string.Format("UPDATE manage\r\nSET KHJE = KHJE - {0}\r\nWHERE Uname = '{1}'\r\n",money,ajg), Connom.Connection);
Comm.Parameters.AddWithValue("@amount", money);
Comm.Parameters.AddWithValue("@accountNumber", ajg);
int rowsAffected_1 = Comm.ExecuteNonQuery();
//转款增钱
SqlCommand CommOne = new SqlCommand(string.Format("UPDATE manage\r\nSET KHJE = KHJE + {0}\r\nWHERE Uname = '{1}'",money,name), Connom.Connection);
CommOne.Parameters.AddWithValue("@amount", money);
CommOne.Parameters.AddWithValue("@accountNumber", name);
int rowsAffected_tow = CommOne .ExecuteNonQuery();
if (rowsAffected_1 > 0 && rowsAffected_tow > 0)
{
Console.WriteLine("转款数据已经更新!");
}
else {
Console.WriteLine("转款数据更新失败!");
}
}
catch (Exception ex)
{
Console.WriteLine("错误", ex.Message);
throw;
}
finally
{
Connom.sqlclose();
}
}
}
}
这里主要是对银行各个功能的代码
最后一个主方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp8
{
internal class Program
{
static void Main(string[] args)
{
Pand pand = new Pand();
pand.directory();
}
}
}