**C#个人理财系统**
一、实验目的
1.初步掌握windows窗体应用程序的设计方法,包括对话框的设计方法;
2.掌握常用窗体控件的使用方法。
3.初步掌握ADO.NET的使用方法,包括Connection 、Command、DataSet、DataAdapter、DataReader对象DataGridView 控件和的使用方法。
4.了解LINQ的基本语法,熟悉LINQ to SQL的使用方法
二、实验内容
设计一个Windows版的个人理财系统,具有用户登录、收支情况管理和基本资料管理等
功能。
三、实验步骤 (记录)
(1).数据库设计
数据库名称:MyAccounting
1.登录用户名以及密码数据库表User:
2,添加收支项目数据课表Addinout:
3.收支情况记录数据库表Inout:
(2).窗体设计
1.登录窗体
相关代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 个人理财.Resources;
namespace 个人理财
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string userName = textBox1.Text;
string password = textBox2.Text;
string connString = "Data Source=.;Initial Catalog=MyAccounting;Persist Security Info=True;User ID=sa;Password=root";
SqlConnection conn = new SqlConnection(connString);
//获取用户名和密码匹配的行的数量的SQL语句
string sql = String.Format("select count(*) from [User] where UserName='{0}'and password='{1}'", userName, password);
try
{
conn.Open();// 打开数据库连接
SqlCommand comm = new SqlCommand(sql, conn); //创建 Command 对象
int n = (int)comm.ExecuteScalar();//执行查询语句,返回匹配的行数
if (n == 1)
{
this.DialogResult = DialogResult.OK;
this.Tag = true;
}
else
{
MessageBox.Show("您输入的用户名或密码错误!请重试", "登录失败",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.Tag = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错!",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.Tag = false;
}
finally
{
conn.Close(); // 关闭数据库连接
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox1.Focus();
}
}
}
2.主界面窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 个人理财.Resources
{
public partial class Main : Form
{
bool isLogined = false; //保存登录凭据
public Main()
{
InitializeComponent();
}
private void toolStripProgressBar1_Click(object sender, EventArgs e)
{
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
if (isLogined)
{
添加收支项目 sForm = new 添加收支项目();
sForm.MdiParent = this;
sForm.Show();
tssMsg.Text = sForm.Text;
}
else
tssMsg.Text = "注意,必须先登录才能使用本系统";
}
private void 添加收支项目PToolStripMenuItem_Click(object sender, EventArgs e)
{
if (isLogined)
{
收支情况记录 cForm = new 收支情况记录();
cForm.MdiParent = this;
cForm.Show();
tssMsg.Text = cForm.Text;
}
else
tssMsg.Text = "注意,必须先登录才能使用本系统";
}
private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
{
关于我们 aForm = new 关于我们();
aForm.MdiParent = this;
aForm.Show();
tssMsg.Text = aForm.Text;
}
private void 用户管理UToolStripMenuItem_Click(object sender, EventArgs e)
{
if (isLogined)
{
Login lForm = new Login();
lForm.MdiParent = this;
lForm.Show();
tssMsg.Text = lForm.Text;
}
else
tssMsg.Text = "注意,必须先登录才能使用本系统";
}
private void 登录LToolStripMenuItem_Click(object sender, EventArgs e)
{
Login UForm = new Login(); //实例化“用户登录”窗体
tssMsg.Text = UForm.Text;
//显示“用户登录”窗体并检测用户是否单击过“登录”按钮
if (UForm.ShowDialog() == DialogResult.OK)
{
if ((bool)UForm.Tag) //如果登录成功
{
isLogined = true;
tssMsg.Text = "恭喜您,已经成功登录系统!";
}
else
{
isLogined = false;
tssMsg.Text = "注意,必须先登录才能使用本系统";
}
}
}
private void menuStrip2_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void 基本资料DToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 收支明细查询ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (isLogined)
{
收支明细查询 cForm = new 收支明细查询();
cForm.MdiParent = this;
cForm.Show();
tssMsg.Text = cForm.Text;
}
else
tssMsg.Text = "注意,必须先登录才能使用本系统";
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
3.收支情况记录窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 个人理财
{
public partial class 收支情况记录 : Form
{
string connString = "Data Source=.;Initial Catalog=MyAccounting;Persist Security Info=True;User ID=sa;Password=root";
public 收支情况记录()
{
InitializeComponent();
//ShowCurrentAccount();
}
private void ShowCurrentAccount()
{
string sql = String.Format("SELECT * FROM Inout");//SQL语句
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open(); //打开数据库连接
SqlCommand comm = new SqlCommand(sql, conn); //创建 Command 对象
SqlDataReader reader = comm.ExecuteReader();
}
}
private void 收支情况记录_Load(object sender, EventArgs e)
{
radioButton1.Checked = true; //默认
comboBox1.SelectedIndex = 0;
dateTimePicker1.MaxDate = DateTime.Now; //设置出生日期最大值为系统当前时间
dateTimePicker1.Value = dateTimePicker1 .MinDate; // //设置出生日期默认值为最小值
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if(label9.Left>=this.Width)
{
label9.Left = 0;
}
label9.Left += 1;
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void label9_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.SelectedIndex)
{
case 3:
listBox1.Items.Clear(); //清除列表框所有项
listBox1.Items.Add("蔬菜");
listBox1.Items.Add("水果");
listBox1.Items.Add("主食");
listBox1.Items.Add("零食");
break;
case 0:
listBox1.Items.Clear();
listBox1.Items.Add("教育培训");
listBox1.Items.Add("买书与杂志");
break;
case 2:
listBox1.Items.Clear();
listBox1.Items.Add("工资");
listBox1.Items.Add("提成");
listBox1.Items.Add("年终奖");
break;
case 1:
listBox1.Items.Clear();
listBox1.Items.Add("房费");
listBox1.Items.Add("水费");
listBox1.Items.Add("电费");
listBox1.Items.Add("交通");
break;
default:
listBox1.Items.Clear();
listBox1.Items.Add("医疗保险");
listBox1.Items.Add("车的保险");
listBox1.Items.Add("理财投资");
break;
}
listBox1.SelectedIndex = 0; //设置默认为第1项
}
private void button1_Click(object sender, EventArgs e)
{
string type1 = "";
if (radioButton1.Checked)
type1 = radioButton1.Text;
else
type1 = radioButton2.Text;
string type2 = comboBox1.SelectedItem.ToString();
string xiangmu = listBox1.SelectedItem.ToString();
string date = dateTimePicker1.Text;
string shuoming = textBox1.Text;
string peo = "";
if (checkBox1.Checked) peo += checkBox1.Text;
if (checkBox2.Checked) peo += "、" + checkBox2.Text;
if (checkBox3.Checked) peo += "、" + checkBox3.Text;
if (checkBox4.Checked) peo += "、" + checkBox4.Text;
if (checkBox5.Checked) peo += "、" + checkBox5.Text;
if (checkBox6.Checked) peo += "、" + checkBox6.Text;
double money = Convert.ToDouble(numericUpDown1.Text);
string beizhu = richTextBox1.Text;
string Message = "您的收支类型是:" + type1
+ "\n收支类别是:" + type2
+ "\n收支项目是:" + xiangmu
+ "\n收支时间为:" + date
+ "\n要添加的说明是:" + shuoming
+ "\n收支人为:" + peo
+ "\n收支金额为" + money
+ "\n备注:" + beizhu;
MessageBox.Show(Message, "收支情况", MessageBoxButtons.OK,
MessageBoxIcon.Information);
string connString = "Data Source=.;Initial Catalog=MyAccounting;Persist Security Info=True;User ID=sa;Password=root";
string sql = String.Format("INSERT INTO Inout(Type1,Type2,Project,Date,Explain,People,Money,Message) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')",
type1, type2,xiangmu,date,shuoming,peo,money,beizhu);
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();// 打开数据库连接
SqlCommand command = new SqlCommand(sql, conn); //创建Command对象
int n = command.ExecuteNonQuery();//执行更新命令,返回值为更新的行数
if (n > 0)
{
MessageBox.Show("添加收支项目成功", "添加成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("添加收支项目失败", "添加失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void domainUpDown1_SelectedItemChanged(object sender, EventArgs e)
{
}
private void process1_Exited(object sender, EventArgs e)
{
}
}
}
4.添加收支项目窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 个人理财
{
public partial class 添加收支项目 : Form
{
string Name;
string Type1;
string Type2;
public 添加收支项目()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
txtInfo.Font = fd.Font;
}
}
private void button4_Click(object sender, EventArgs e)//颜色
{
ColorDialog cd = new ColorDialog();
if (cd.ShowDialog() == DialogResult.OK)
{
txtInfo.BackColor = cd.Color;
}
}
private void 添加收支项目_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim().Length == 0)
{
MessageBox.Show("输入信息不完整!", "信息不完整", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
tabControl1.SelectedTab = tabPage2;
// tabControl1.SelectedIndex = 1;
}
}
private void tabPage2_Click(object sender, EventArgs e)
{
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void tabControl1_SelectedIndexChanged_1(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex == 1)
{
if (textBox1.Text.Trim().Length == 0)
{
MessageBox.Show("输入信息不完整!", "信息不完整", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
tabControl1.SelectedIndex = 0;
}
else
{
Name = textBox1.Text;
if (radioButton1.Checked)
Type1 = radioButton1.Text;
else
Type1 = radioButton2.Text;
Type2 = comboBox1.SelectedItem.ToString();
string info = String.Format("要添加的收支项目为:{0}\n所属类别是:{1}\n是{2}\n类型的项目", Name, Type1, Type2);
txtInfo.Text = info;
}
}
}
private void btnYes_Click(object sender, EventArgs e)
{
string connString = "Data Source=.;Initial Catalog=MyAccounting;Persist Security Info=True;User ID=sa;Password=root";
string sql = String.Format("INSERT INTO Addinout(Project, Type1,Type2) VALUES('{0}','{1}','{2}')", Name,Type1,Type2);
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();// 打开数据库连接
SqlCommand command = new SqlCommand(sql, conn); //创建Command对象
int n = command.ExecuteNonQuery();//执行更新命令,返回值为更新的行数
if (n > 0)
{
MessageBox.Show("添加收支项目成功", "添加成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("添加收支项目失败", "添加失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
}
5.收支明细查询窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 个人理财
{
public partial class 收支明细查询 : Form
{
string connString = "Data Source=.;Initial Catalog=MyAccounting;Persist Security Info=True;User ID=sa;Password=root";
public 收支明细查询()
{
InitializeComponent();
}
private SqlDataAdapter da = new SqlDataAdapter();
private DataSet ds = new DataSet("MyAccounting");
private void button1_Click(object sender, EventArgs e)
{
int index=comboBox1.SelectedIndex;
string connString = "Data Source=.;Initial Catalog=MyAccounting;Persist Security Info=True;User ID=sa;Password=root";
string a = textBox1.Text;
string b = textBox2.Text;
if (index==0)
{
string str = comboBox1.SelectedText;
string sql = "select Type1 as 类别,Type2 as 类型,Project as 项目,Date as 日期,Explain as 说明,People as 收支人," +
" Money as 金额,Message as 备注 from Inout where Project='"+ textBox1.Text + "'";
SqlConnection conn = new SqlConnection(connString);
SqlCommand command = new SqlCommand(sql, conn); //创建Command对象
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
da.SelectCommand = comm;
SqlCommandBuilder builder = new SqlCommandBuilder(da);
da.Fill(ds,"Inout");
search.DataSource = ds.Tables["Inout"];
conn.Close();
}
if (index == 1)
{
string str = comboBox1.SelectedText;
string sql = "select Type1 as 类别,Type2 as 类型,Project as 项目,Date as 日期,Explain as 说明,People as 收支人," +
" Money as 金额,Message as 备注 from Inout where Type1='" + textBox1.Text + "'";
SqlConnection conn = new SqlConnection(connString);
SqlCommand command = new SqlCommand(sql, conn); //创建Command对象
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
da.SelectCommand = comm;
SqlCommandBuilder builder = new SqlCommandBuilder(da);
da.Fill(ds, "Inout");
search.DataSource = ds.Tables["Inout"];
conn.Close();
}
if (index == 2)
{
string str = comboBox1.SelectedText;
string sql = "select Type1 as 类别,Type2 as 类型,Project as 项目,Date as 日期,Explain as 说明,People as 收支人," +
" Money as 金额,Message as 备注 from Inout where Type2='" + textBox1.Text + "'";
SqlConnection conn = new SqlConnection(connString);
SqlCommand command = new SqlCommand(sql, conn); //创建Command对象
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
da.SelectCommand = comm;
SqlCommandBuilder builder = new SqlCommandBuilder(da);
da.Fill(ds, "Inout");
search.DataSource = ds.Tables["Inout"];
conn.Close();
}
if (index == 3)
{
string str = comboBox1.SelectedText;
string sql = "select Type1 as 类别,Type2 as 类型,Project as 项目,Date as 日期,Explain as 说明,People as 收支人," +
" Money as 金额,Message as 备注 from Inout where Money>='" + textBox1.Text + "' and Money<='" + textBox2.Text + "'";
SqlConnection conn = new SqlConnection(connString);
SqlCommand command = new SqlCommand(sql, conn); //创建Command对象
conn.Open();
SqlCommand comm = new SqlCommand(sql, conn);
da.SelectCommand = comm;
SqlCommandBuilder builder = new SqlCommandBuilder(da);
da.Fill(ds, "Inout");
search.DataSource = ds.Tables["Inout"];
conn.Close();
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
6.关于我们窗体
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 个人理财
{
public partial class 关于我们 : Form
{
public 关于我们()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}