记录一下自己做的项目,第一次用c#写项目还有很多不足,大神们多多指教
我的个人网站地址(域名备案还没通过):http://123.56.100.46/
欢迎访问!!
上面是文档的大概内容,有些写的还不够全面,大致包含了软件开发一些必须的步骤,虽然这个项目仅仅是练习,但俗话说的好–“练为战,不为看!”,在这些方面尽量做的完整一些没啥坏处。
(1)要求系统具有良好的人机交互界面
(2)对进货和库存信息进行有效的管理
(3)对用户的信息进行有效的管理
(4)对用户输入的信息可以进行有效的检验
(5)对超市销售信息进行有效的存储和管理
(6)对超市库存信息进行有效的存储和管理
(7)对超市进货信息进行有效的存储和管理
(8)强大的报表打印功能
(9)对用户输入的数据,进行严格的数据检验,尽可能避免人为的错误
(10)系统开发帮助文档,方便用户在遇到疑问是随时查阅
(11)有用户权限的区别,管理员才可以对数据库中用户进行增删改,而普通用户不可以,普通用户只能修改自己的信息。
数据库中我做了四个表,分别是库存信息表、销售信息表、进货信息表、员工信息表。
下一步要做的就是开始写代码,所以在这之前我把写过的功能分析更细化了一下,删除了一些我目前还没法实现的功能,保留了一些必要的功能
登陆界面这里是没有做选择用户类型的,我写了一个静态的全局变量来保存用户的信息,在输入用户名密码的时候自动识别用户类型(管理员/普通用户)
代码:
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
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;
using System.Data.OleDb;
using static WindowsForms0929.Program;
namespace WindowsForms0929
{
public partial class Form2 : Form
{
public static string name = “”;
public static string role = “”;
public static string passwd = “”;
public Form2()
{
InitializeComponent();
}
public static int count = 1;
public static string strUserName, strUserPwd, strUserRole;
//public class Userinfo
//{
// public static string uname = “”;
// public static string urole = “”;
//}
private void Form2_Load(object sender, EventArgs e)
{
txt_id.SkinFile = “skin\mp10maroon.ssk”;
}
private void button1_Click(object sender, EventArgs e)
{
int count = 1;
if (text_id.Text.Trim() == "")
{
MessageBox.Show("用户名不能为空!");
text_id.Focus();
count++;
return;
}
if (text_passwd.Text.Trim() == "")
{
MessageBox.Show("密码不能为空!");
text_passwd.Focus();
count++;
return;
}
//OleDbConnection conn = new OleDbConnection();
//conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\\Database1.accdb;";
//string Sqlstr = "select count(*) from tb_EmpInfo where emp_login_name='" + text_id.Text
//+ "'" + " and emp_login_passwd='" + text_passwd.Text + "'";
//OleDbCommand cmd = new OleDbCommand(Sqlstr, conn);
//if (conn.State == ConnectionState.Closed)
//{
// try
// {
// conn.Open();
// }
// catch (Exception ex)
// {
// MessageBox.Show(ex.ToString());
// return;
// }
//}
//OleDbDataReader odr = cmd.ExecuteReader();
//while (textBox1.Text.Trim()!=""&& textBox2.Text.Trim() != "")
//{
// if ((Convert.ToInt32(cmd.ExecuteScalar())) > 0)
// {
// Form1 frm = new Form1();
// frm.Show();
// this.Hide();
// }
// else
// {
// count++;
// MessageBox.Show("用户名或密码错误");
// textBox1.Text = "";
// textBox2.Text = "";
// textBox1.Focus();
// }
//}
//if (odr.HasRows)
//{
// odr.Read();
// if (odr[2].ToString().Trim() == text_passwd.Text.Trim())
// {
// strUserName = odr[1].ToString();
// strUserPwd = odr[3].ToString();
// strUserRole = odr[4].ToString();
// Form3 frm = new Form3();
// frm.Show();
// this.Hide();
// }
// else
// {
// MessageBox.Show("密码错误,请输入正确的密码!");
// text_passwd.Text = "";
// text_passwd.Focus();
// count++;
// return;
// }
//}
//else
//{
// MessageBox.Show("用户名错误!");
// text_id.Text = "";
// text_passwd.Text = "";
// text_id.Focus();
// count++;
// return;
//}
//if (count > 2)
//{
// button1.Enabled = false;
//}
//return;
if (text_id.Text == "")
{
MessageBox.Show("用户名不能为空");
return;
}
if (text_passwd.Text == "")
{
MessageBox.Show("密码不能为空");
return;
}
string sqlstr = "select count(*) from tb_EmpInfo where emp_login_name='" + text_id.Text
+ "'" + " and emp_login_passwd='" + text_passwd.Text + "'";
publicClass.getSqlConnection get = new publicClass.getSqlConnection();
OleDbConnection conn = get.GetCon();
OleDbCommand cmd = new OleDbCommand
{
Connection = conn,
CommandType = CommandType.Text,
CommandText = sqlstr
};
if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)
{
//得到用户名
name = text_id.Text.Trim();
passwd = text_passwd.Text.Trim();
cmd.CommandText = "select emp_role from tb_EmpInfo where emp_login_name='" + text_id.Text
+ "'" + " and emp_login_passwd='" + text_passwd.Text + "'";
//得到用户角色
role = cmd.ExecuteScalar().ToString();
Userinfo.uname = text_id.Text.Trim();
Userinfo.urole = cmd.ExecuteScalar().ToString();
MessageBox.Show("恭喜你\n"+role+"\n登陆成功!!");
Form3 frm = new Form3();
frm.Show();
this.Hide();
}
else
{
MessageBox.Show("用户名或密码错误!请重试");
return;
}
}
private void Form2_FormClosing(object sender,FormClosedEventArgs e)
{
DialogResult dr = MessageBox.Show("是否关闭窗体?","提示",MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (dr == DialogResult.Yes)
{
Application.ExitThread();
}
}
private void text_id_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void pictureBox3_Click(object sender, EventArgs e)
{
MessageBox.Show("原来你也喜欢喝可乐,快加入我们吧!!");
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("是否关闭窗体?", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (dr == DialogResult.Yes)
{
Application.ExitThread();
}
}
protected override void OnClosing(CancelEventArgs e)
{
DialogResult result = MessageBox.Show("确定退出窗口?", "警告",
MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
e.Cancel = result != DialogResult.Yes;
base.OnClosing(e);
}
}
}
program.cs中的代码:
当用户登陆的时候自动识别用户权限,我用了一个messagebox来显示
主界面大家可以看到很多功能,还有一些小工具,比如说旁边的计算机、记事本等
下面的状态栏显示的也是用户的权限信息
代码:
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
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;
using System.Runtime.InteropServices;
using static WindowsForms0929.Program;
namespace WindowsForms0929
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void 用户信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 fm1 = new Form1();
fm1.ShowDialog();
fm1.Owner = this;
}
private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox1 ab = new AboutBox1();
ab.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("calc.exe");
}
[DllImport("user32 ")]
public static extern bool LockWorkStation();
private void button1_Click_1(object sender, EventArgs e)
{
LockWorkStation();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void 进货管理ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form4 frm4 = new Form4();
frm4.ShowDialog();
frm4.Owner = this;
}
private void 库存管理ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form5 frm5 = new Form5();
frm5.ShowDialog();
frm5.Owner = this;
}
private void 销售管理ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form6 frm6 = new Form6();
frm6.ShowDialog();
frm6.Owner = this;
}
private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form7 frm7 = new Form7();
frm7.ShowDialog();
frm7.Owner = this;
}
private void 其他报表ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form8 frm8 = new Form8();
frm8.ShowDialog();
frm8.Owner = this;
}
private void 使用手册ToolStripMenuItem_Click(object sender, EventArgs e)
{
Help.ShowHelp(this, "help2.chm");
}
private void 管理ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void 管理员报表ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form10 frm10 = new Form10();
frm10.ShowDialog();
frm10.Owner = this;
}
private void Form3_Load(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = Userinfo.uname;
toolStripStatusLabel2.Text = Userinfo.urole;
toolStripStatusLabel3.Text = DateTime.Now.ToString();
}
private void 进货报表ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form9 frm9 = new Form9();
frm9.ShowDialog();
frm9.Owner = this;
}
private void button5_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("notepad.exe");
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("notepad.exe");
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("calc.exe");
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("mspaint.exe");
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
Help.ShowHelp(this, "help2.chm");
}
private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
//[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
private void MainForm_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void 锁屏ToolStripMenuItem_Click(object sender, EventArgs e)
{
LockWorkStation();
}
private void 重新登陆ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void button6_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void 缩小窗体ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
private void 最小化ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void 放大窗体ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("确定退出软件吗?", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (dr == DialogResult.Yes)
{
Application.Exit();
}
}
private void button4_Click_1(object sender, EventArgs e)
{
Application.Exit();
}
private void button3_Click_1(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("calc.exe");
}
private void button1_Click_2(object sender, EventArgs e)
{
LockWorkStation();
}
private void button2_Click_1(object sender, EventArgs e)
{
Application.Restart();
}
private void button5_Click_1(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("notepad.exe");
}
private void button6_Click_1(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void pictureBox3_Click(object sender, EventArgs e)
{
MessageBox.Show("原来你也喜欢喝可乐,快加入我们吧!!");
}
private void pictureBox2_Click(object sender, EventArgs e)
{
MessageBox.Show("原来你也喜欢喝可乐,快加入我们吧!!");
}
private void button7_Click(object sender, EventArgs e)
{
Form12 frm12 = new Form12();
frm12.ShowDialog();
frm12.Owner = this;
}
protected override void OnClosing(CancelEventArgs e)
{
DialogResult dr = MessageBox.Show("确定退出系统吗?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if(dr!=DialogResult.Yes)
{
e.Cancel = true;
}
base.OnClosing(e);
}
private void 软件帮助文档RToolStripMenuItem_Click(object sender, EventArgs e)
{
Help.ShowHelp(this, "help.chm");
}
}
}
这个模块包含两个功能,一个是查询用户的基本信息,一个是修改当前用户登陆的密码
用户基本信息:
点击刷新按钮:在右边的gridview中显示数据库中所有用户信息
点击添加用户按钮:要使这个按钮有效,首先要完善上面textbox框中的内容,否则会提示完善信息(注意: 这里只有管理员用户才有权限,普通用户是没法添加用户信息的)
点击删除用户:在右侧的gridview中选中用户,点击删除按钮该用户的信息就会被删除(注意: 这里只有管理员用户才有权限,普通用户是没法删除用户信息的)
点击统计按钮可以统计所有管理员和普通用户的信息
代码:
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
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;
using System.Data.OleDb;
using WindowsForms0929.publicClass;
using static WindowsForms0929.Program;
namespace WindowsForms0929
{
public partial class Form1 : Form
{
public static string name = "";
public static string role = "";
public static string passwd = "";
DataSet ds;
OleDbDataAdapter da;
publicClass.getSqlConnection get;
OleDbConnection conn;
OleDbCommand cmd;
OleDbCommand cmd2;
DataTable dt;
string conststr = "select emp_name as 姓名,emp_login_name as 登录名," +
"emp_login_passwd as 登录密码,emp_role as 用户角色,emp_phone as 联系电话" +
" from tb_EmpInfo";
public Form1()
{
InitializeComponent();
}
private void Qingkong()
{//清空文本框的内容
txt_login_name.Clear();
txt_login_passwd.Clear();
txt_name.Clear();
txt_phone.Clear();
}
public int count = 2;
private void button1_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\\Database1.accdb;";
//Trim--去掉输入框中读取到的空格
string Strsda1 = "select * from tb_user where user_Name like'%" + textBox1.Text.Trim() + "%'";
string Strsda2 = "select * from tb_user where user_Name='%" + textBox1.Text.Trim() + "%'";
OleDbCommand cmd = new OleDbCommand(Strsda1, conn);
conn.Open();
OleDbDataAdapter sda1 = new OleDbDataAdapter(Strsda1,conn);
OleDbDataAdapter sda2 = new OleDbDataAdapter(Strsda2,conn);
DataSet ds = new DataSet();
sda1.Fill(ds, "Tb_userinfo1");
sda2.Fill(ds, "Tb_userinfo2");
dataGridView1.DataSource = ds.Tables["Tb_userinfo1"];
}
private void Form1_Load(object sender, EventArgs e)
{
}
//private void button2_Click(object sender, EventArgs e)
//{
// OleDbConnection conn = new OleDbConnection();
// conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\\Database1.accdb;";
// string Strsda1 = "select * from tb_user where user_role='经理'";
// OleDbDataAdapter sda1 = new OleDbDataAdapter(Strsda1, conn);
// DataSet myds = new DataSet();
// sda1.Fill(myds);
// dataGridView1.DataSource = myds.Tables[0];
// conn.Open();
// //int count = Convert.ToInt32(cmd.ExecuteScalar());
// //DataSet ds = new DataSet();
// //sda1.Fill(ds, "Tb_userinfo1");
// //dataGridView1.DataSource = ds.Tables["Tb_userinfo1"];
// //textBox2.Text = Convert.ToString(count+1);
//}
protected override void OnClosing(CancelEventArgs e)
{
DialogResult result = MessageBox.Show("确定退出窗口?", "警告",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
e.Cancel = result != DialogResult.Yes;
base.OnClosing(e);
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
private void btn_qingkong_Click(object sender, EventArgs e)
{
Qingkong();
string str = conststr;
get = new publicClass.getSqlConnection();
conn = get.GetCon();
cmd = new OleDbCommand()
{
CommandText = str,
Connection = conn
};
ds = new DataSet();
da = new OleDbDataAdapter
{
SelectCommand = cmd
};
da.Fill(ds, "tb_EmpInfo");
dataGridView2.DataSource = ds.Tables["tb_EmpInfo"];
conn.Dispose();
cmd.Dispose();
da.Dispose();
ds.Dispose();
}
private void button1_Click_1(object sender, EventArgs e)
{
////得到用户名
//name = text_id.Text.Trim();
//passwd = text_passwd.Text.Trim();
//cmd.CommandText = "select emp_role from tb_EmpInfo where emp_login_name='" + text_id.Text
//+ "'" + " and emp_login_passwd='" + text_passwd.Text + "'";
////得到用户角色
//role = cmd.ExecuteScalar().ToString();
if (Userinfo.urole == "管理员") {
if (txt_name.Text.Equals(""))
{
MessageBox.Show("用户名字未输入");
return;
}
if (txt_login_name.Text.Equals(""))
{
MessageBox.Show("用户登录名未输入");
return;
}
if (txt_phone.Text.Equals(""))
{
MessageBox.Show("用户电话未输入");
return;
}
if (txt_login_passwd.Text.Equals(""))
{
MessageBox.Show("用户密码未输入");
return;
}
string str_insert = "insert into tb_EmpInfo(emp_name,emp_login_name,emp_login_passwd,emp_role,emp_phone) " +
"values('" +
txt_name.Text.Trim() + "','" +
txt_login_name.Text.Trim() + "','" +
txt_login_passwd.Text.Trim() + "','" +
txt_role.SelectedItem.ToString().Trim() + "','" +
txt_phone.Text.Trim() + "'" +
")";
get = new publicClass.getSqlConnection();
conn = get.GetCon();
cmd = new OleDbCommand
{
Connection = conn,
CommandText = str_insert
};
cmd.ExecuteNonQuery();
//更新cmd的SQL语句
cmd.CommandText = conststr;
ds = new DataSet();
da = new OleDbDataAdapter
{
SelectCommand = cmd
};
da.Fill(ds, "tb_EmpInfo");
dataGridView2.DataSource = ds.Tables["tb_EmpInfo"];
cmd.Dispose();
da.Dispose();
ds.Dispose();
MessageBox.Show("添加成功!");
//dataGridView1.Update();
}
else
{
MessageBox.Show("权限未达到,详情咨询管理员!");
}
}
private void button6_Click(object sender, EventArgs e)
{
if (Userinfo.urole == "管理员") {
string str = dataGridView2.CurrentRow.Cells[1].Value.ToString().Trim();
get = new publicClass.getSqlConnection();
conn = get.GetCon();
cmd = new OleDbCommand
{
CommandText = "delete from tb_empInfo where emp_login_name='" + str + "'",
Connection = conn
};
cmd.ExecuteNonQuery();
cmd.CommandText = conststr;
ds = new DataSet();
da = new OleDbDataAdapter
{
SelectCommand = cmd
};
da.Fill(ds, "tb_EmpInfo");
dataGridView2.DataSource = ds.Tables["tb_EmpInfo"];
conn.Dispose();
cmd.Dispose();
da.Dispose();
ds.Dispose();
dataGridView2.Update();
MessageBox.Show("删除" + str + "成功");
}
else
{
MessageBox.Show("权限不够,详情咨询管理员!!");
}
}
public static string newPasswd;
private void button7_Click(object sender, EventArgs e)
{
if (txt_login_name.Text.Equals("") && txt_name.Text.Equals(""))
{
MessageBox.Show("请输入姓名或用户登录名!");
return;
}
if (txt_login_name.Text.Equals("") == false)
{
string sql = conststr + " where emp_login_name='"
+ txt_login_name.Text.Trim()
+ "'";
get = new publicClass.getSqlConnection();
conn = get.GetCon();
cmd = new OleDbCommand
{
CommandText = sql,
Connection = conn
};
da = new OleDbDataAdapter
{
SelectCommand = cmd
};
//da.SelectCommand.ExecuteNonQuery();
ds = new DataSet();
da.Fill(ds, "tb_EmpInfo");
dt = ds.Tables["tb_EmpInfo"];
dataGridView1.DataSource = ds.Tables["tb_EmpInfo"];
cmd.Dispose();
ds.Dispose();
dt.Dispose();
conn.Dispose();
}
else
{
string sql = conststr + " where emp_name='"
+ txt_name.Text.Trim()
+ "'";
get = new publicClass.getSqlConnection();
conn = get.GetCon();
cmd = new OleDbCommand(sql, conn);
da = new OleDbDataAdapter(sql, conn);
//da.SelectCommand.ExecuteNonQuery();
ds = new DataSet();
da.Fill(ds, "tb_EmpInfo");
dt = ds.Tables["tb_EmpInfo"];
dataGridView2.DataSource = dt;
}
}
private void button8_Click(object sender, EventArgs e)
{
get = new publicClass.getSqlConnection();
conn = get.GetCon();
string admin = "select count(*) from tb_EmpInfo where emp_role='管理员'";
string normal = "select count(*) from tb_EmpInfo where emp_role='普通用户'";
cmd = new OleDbCommand(admin, conn);
txt_admin_num.Text = cmd.ExecuteScalar().ToString();
cmd.CommandText = normal;
txt_normal_num.Text = cmd.ExecuteScalar().ToString();
}
private void Form1_Load_1(object sender, EventArgs e)
{
//获取数据
get = new publicClass.getSqlConnection();
conn = get.GetCon();
cmd = new OleDbCommand
{
CommandText = conststr,
Connection = conn
};
ds = new DataSet();
da = new OleDbDataAdapter
{
SelectCommand = cmd
};
da.Fill(ds, "tb_EmpInfo");
dataGridView2.DataSource = ds.Tables["tb_EmpInfo"];
//释放资源
conn.Dispose();
cmd.Dispose();
da.Dispose();
ds.Dispose();
}
private void txt_normal_num_TextChanged(object sender, EventArgs e)
{
}
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void txt_role_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
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;
using System.Data.OleDb;
namespace WindowsForms0929
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (text_old_passwd.Text.Trim().Equals(Form2.passwd) == false)
{
MessageBox.Show("原密码错误!记不得密码不要点修改密码,麻烦退出点击‘忘记密码’!");
return;
}
if (text_new_passwd.Text.ToString().Equals(""))
{
MessageBox.Show("请输入你的新密码!");
return;
}
string sqlstr = "update tb_EmpInfo set emp_login_passwd='" + text_new_passwd.Text.Trim() + "'"
+ "where emp_login_name='" + Form2.name + "'";
try
{
publicClass.getSqlConnection get = new publicClass.getSqlConnection();
OleDbConnection conn = get.GetCon();
OleDbCommand cmd = new OleDbCommand(sqlstr, conn);
OleDbDataAdapter da = new OleDbDataAdapter();
cmd.ExecuteNonQuery();
MessageBox.Show("密码修改成功!!请牢记新密码!!!");
this.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
//MessageBox.Show("");
this.Close();
}
private void Form7_Load(object sender, EventArgs e)
{
}
}
}
管理实现的是三个功能,基本就是超市的进销存的功能。
进货管理:
输入商品的名称、数量、价格等基本信息,点击添加添加商品到库存,这里是直接在数据库中修改的。
代码:
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
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;
using System.Data.OleDb;
namespace WindowsForms0929
{
public partial class Form4 : Form
{
DataSet ds;
OleDbDataAdapter da;
publicClass.getSqlConnection get;
OleDbConnection conn;
OleDbCommand cmd;
string selectStr = "select goods_id as 商品编号,emp_name as 操作员名称,goods_name as" +
" 商品名称,goods_num as 商品数量,goods_buy_price as 进货单价,goods_sell_price as 销售单价," +
"goods_time as 进货时间,goods_remark as 备注 from tb_JhGoodsInfo";
public Form4()
{
InitializeComponent();
timer1.Enabled = true;
timer1.Interval = 200;
}
public string getTime()
{
return DateTime.Now.ToString().Trim();
}
public string getID()
{
int year = DateTime.Now.Year;
int month = DateTime.Now.Month;
int day = DateTime.Now.Day;
int hour = DateTime.Now.Hour;
int second = DateTime.Now.Second;
int minute = DateTime.Now.Minute;
int minminute = DateTime.Now.Millisecond;
string strTime = null;
strTime = year.ToString();
strTime += month >= 10 ? month.ToString() : "0" + month.ToString();
strTime += day >= 10 ? day.ToString() : "0" + day.ToString();
strTime += hour >= 10 ? hour.ToString() : "0" + hour.ToString();
strTime += second >= 10 ? second.ToString() : "0" + second.ToString();
strTime += minute >= 10 ? minute.ToString() : "0" + minute.ToString();
strTime += minminute >= 10 ? minminute.ToString() : "0" + minminute.ToString();
return "yyj-" + strTime;
}
private void Form4_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if(text_in_price.Text.Equals(""))
{
MessageBox.Show("请输入进货单价");
return;
}
if (text_name.Text.Equals(""))
{
MessageBox.Show("请输入商品名称");
return;
}
if (text_num.Text.Equals(""))
{
MessageBox.Show("请输入商品数量");
return;
}
if (text_out_price.Text.Equals(""))
{
MessageBox.Show("请输入销售单价");
return;
}
string nowtime = getTime();
string id = getID();
//数据库连接,进货数据库
string insertstr_jh = "insert into tb_JhGoodsInfo(goods_id,emp_name,goods_name," +
"goods_num,goods_buy_price,goods_sell_price,goods_time,goods_remark) " +
"values('" + id + "','" + Form2.name + "','" + text_name.Text.ToString().Trim() + "',"
+ text_num.Text + "," + Convert.ToInt32(text_in_price.Text) + "," + Convert.ToInt32(text_out_price.Text) + ",'"
+ nowtime
+ "','"+ "')";
get = new publicClass.getSqlConnection();
conn = get.GetCon();
cmd = new OleDbCommand(insertstr_jh, conn);
da = new OleDbDataAdapter();
da.InsertCommand = cmd;
da.InsertCommand.ExecuteNonQuery();
da.SelectCommand = new OleDbCommand(selectStr, conn);
ds = new DataSet();
da.Fill(ds, "tb_JhGoodsInfo");
dataGridView1.DataSource = ds.Tables["tb_JhGoodsInfo"];
ds.Dispose();
cmd.CommandText = selectStr;
da.SelectCommand = cmd;
da.SelectCommand.ExecuteNonQuery();
ds = new DataSet();
da.Fill(ds, "tb_JhGoodsInfo");
dataGridView1.DataSource = ds.Tables["tb_JhGoodsInfo"];
//数据库操作,库存数据库
string insertstr_kc = "insert into tb_KcGoods(goods_id,rep_emp,rep_goods_name," +
"rep_num,rep_goods_price,rep_sell_price,rep_time,rep_remark) " +
"values('" + getID() + "','" + Form2.name + "','" + text_name.Text.ToString().Trim() + "',"
+ text_num.Text + "," + Convert.ToInt32(text_in_price.Text) + "," + Convert.ToInt32(text_out_price.Text) + ",'"
+ nowtime
+ "','" + "')";
string updatestr_kc = "update tb_KcGoods set rep_num=rep_num+" + Convert.ToInt32(text_num.Text)
+ " where rep_goods_name='" + text_name.Text.ToString().Trim() + "'";
string select_kc = "select count(*) from tb_KcGoods where rep_goods_name='" + text_name.Text.ToString().Trim() + "'";
conn = get.GetCon();
cmd = new OleDbCommand(select_kc, conn);
if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)
{//如果库存已有,则新增update
cmd = new OleDbCommand(updatestr_kc, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("增加" + text_name.Text.ToString().Trim() +
"数量" + Convert.ToInt32(text_num.Text).ToString());
}
else
{
cmd = new OleDbCommand(insertstr_kc, conn);
int s = cmd.ExecuteNonQuery();
MessageBox.Show("插入" + text_name.Text.ToString().Trim() +
"数量" + Convert.ToInt32(text_num.Text).ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
if (MessageBox.Show("是否删除?", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
== DialogResult.OK)
{
}
else
{
return;
}
string str = dataGridView1.CurrentRow.Cells[1].Value.ToString();
string name = dataGridView1.CurrentRow.Cells[2].Value.ToString();
get = new publicClass.getSqlConnection();
conn = get.GetCon();
cmd = new OleDbCommand("delete from tb_JhGoodsInfo where goods_id='" + str + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
cmd.Dispose();
get = new publicClass.getSqlConnection();
conn = get.GetCon();
cmd = new OleDbCommand(selectStr, get.GetCon());
ds = new DataSet();
da = new OleDbDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds, "tb_JhGoodsInfo");
dataGridView1.DataSource = ds.Tables["tb_JhGoodsInfo"];
conn.Dispose();
cmd.Dispose();
da.Dispose();
ds.Dispose();
dataGridView1.Update();
MessageBox.Show("编号:" + str + "\n商品名称:" + name + "\n已删除");
}
private void button3_Click(object sender, EventArgs e)
{
text_out_price.Clear();
text_num.Clear();
text_name.Clear();
text_in_price.Clear();
get = new publicClass.getSqlConnection();
conn = get.GetCon();
cmd = new OleDbCommand(selectStr, conn);
ds = new DataSet();
da = new OleDbDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds, "tb_JhGoodsInfo");
dataGridView1.DataSource = ds.Tables["tb_JhGoodsInfo"];
conn.Dispose();
cmd.Dispose();
da.Dispose();
ds.Dispose();
}
}
}
库存管理:
库存管理这里只做了两个按钮,分别实现查询和刷新的功能
根据商品名称对商品的所有信息进行查询,包括价格
代码:
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
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;
using System.Data.OleDb;
namespace WindowsForms0929
{
public partial class Form5 : Form
{
DataSet ds;
OleDbDataAdapter da;
publicClass.getSqlConnection get;
OleDbConnection conn;
OleDbCommand cmd;
//DataTable dt;
string conststr = "select goods_id as 商品编号,rep_emp as 进货人,rep_goods_name as 商品名称," +
"rep_num as 库存数量,rep_goods_price as 进货单价,rep_sell_price as 销售价格,rep_time as" +
" 进货时间,rep_remark as 备注 from tb_KcGoods";
public Form5()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string str = conststr + " where rep_goods_name='" + textBox1.Text.Trim() + "'";
get = new publicClass.getSqlConnection();
conn = get.GetCon();
cmd = new OleDbCommand(str, conn);
ds = new DataSet();
da = new OleDbDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds, "tb_KcGoods");
dataGridView1.DataSource = ds.Tables["tb_KcGoods"];
conn.Dispose();
cmd.Dispose();
da.Dispose();
ds.Dispose();
}
private void Form5_Load(object sender, EventArgs e)
{
//this.FormBorderStyle = FormBorderStyle.FixedDialog;
//this.MaximizeBox = false;
//this.CenterToScreen();
//this.BackColor = Color.LightBlue;
//dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
//dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
//dataGridView1.GridColor = Color.LightBlue;
//dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightBlue;
//dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.LightCyan;
//dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
get = new publicClass.getSqlConnection();
conn = get.GetCon();
cmd = new OleDbCommand(conststr, conn);
ds = new DataSet();
da = new OleDbDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds, "tb_KcGoods");
dataGridView1.DataSource = ds.Tables["tb_KcGoods"];
conn.Dispose();
cmd.Dispose();
da.Dispose();
ds.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
get = new publicClass.getSqlConnection();
conn = get.GetCon();
cmd = new OleDbCommand(conststr, conn);
ds = new DataSet();
da = new OleDbDataAdapter();
da.SelectCommand = cmd;
da.Fill(ds, "tb_KcGoods");
dataGridView1.DataSource = ds.Tables["tb_KcGoods"];
conn.Dispose();
cmd.Dispose();
da.Dispose();
ds.Dispose();
}
}
}
销售管理:
输入你要销售的商品和销售的数量,就可以直接将库存中的商品卖掉,这里也是直接在数据库中修改。
代码:
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
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;
using System.Data.OleDb;
namespace WindowsForms0929
{
public partial class Form6 : Form
{
DataSet ds;
OleDbDataAdapter da;
publicClass.getSqlConnection get;
OleDbConnection conn;
OleDbCommand cmd;
string select_str = "select goods_id as 商品编号,rep_emp as 进货人,rep_goods_name as 商品名称," +
"rep_num as 库存数量,rep_goods_price as 进货单价,rep_sell_price as 销售价格,rep_time as" +
" 进货时间,rep_remark as 备注 from tb_KcGoods";
public Form6()
{
InitializeComponent();
}
//获取当前操作id
public string GetID()
{
int year = DateTime.Now.Year;
int month = DateTime.Now.Month;
int day = DateTime.Now.Day;
int hour = DateTime.Now.Hour;
int second = DateTime.Now.Second;
int minute = DateTime.Now.Minute;
int minminute = DateTime.Now.Millisecond;
string strTime = null;
strTime = year.ToString();
strTime += month >= 10 ? month.ToString() : "0" + month.ToString();
strTime += day >= 10 ? day.ToString() : "0" + day.ToString();
strTime += hour >= 10 ? hour.ToString() : "0" + hour.ToString();
strTime += second >= 10 ? second.ToString() : "0" + second.ToString();
strTime += minute >= 10 ? minute.ToString() : "0" + minute.ToString();
strTime += minminute >= 10 ? minminute.ToString() : "0" + minminute.ToString();
return "yyj-" + strTime;
}
//销售时获取当前时间
public string GetTime()
{
return DateTime.Now.ToString().Trim();
}
private void Form6_Load(object sender, EventArgs e)
{
get = new publicClass.getSqlConnection();
conn = get.GetCon();
cmd = new OleDbCommand(select_str, conn);
ds = new DataSet();
da = new OleDbDataAdapter
{
SelectCommand = cmd
};
da.Fill(ds, "tb_SellGoods");
dataGridView1.DataSource = ds.Tables["tb_SellGoods"];
conn.Dispose();
cmd.Dispose();
da.Dispose();
ds.Dispose();
}
private void button1_Click(object sender, EventArgs e)
{
string str = select_str + " where rep_goods_name='" + text_name.Text.Trim() + "'";
get = new publicClass.getSqlConnection();
conn = get.GetCon();
cmd = new OleDbCommand(str, conn);
ds = new DataSet();
da = new OleDbDataAdapter
{
SelectCommand = cmd
};
da.Fill(ds, "tb_KcGoods");
dataGridView1.DataSource = ds.Tables["tb_KcGoods"];
conn.Dispose();
cmd.Dispose();
da.Dispose();
ds.Dispose();
}
private void button2_Click(object sender, EventArgs e)
{
text_name.Clear();
text_num.Clear();
}
private void button3_Click(object sender, EventArgs e)
{
if (text_name.Text.Equals("") || text_num.Text.Equals(""))
{
MessageBox.Show("请输入!!");
return;
}
get = new publicClass.getSqlConnection();
conn = get.GetCon();
string str_select = "select count(*) from tb_KcGoods where rep_goods_name='" +
text_name.Text.Trim() + "'";
cmd = new OleDbCommand(str_select, conn);
if (Convert.ToInt32(cmd.ExecuteScalar()) <= 0)
{
MessageBox.Show("仓库中查无此物!!!");
cmd.Dispose();
return;
}
string update_str = "update tb_KcGoods set rep_num=rep_num-"
+ Convert.ToInt32(text_num.Text.Trim()) + " where rep_goods_name='" +
text_name.Text.Trim() + "'";
cmd = new OleDbCommand(update_str, conn);
cmd.ExecuteNonQuery();
cmd.CommandText = select_str;
ds = new DataSet();
da = new OleDbDataAdapter
{
SelectCommand = cmd
};
da.Fill(ds, "tb_KcGoods");
dataGridView1.DataSource = ds.Tables["tb_KcGoods"];
string id = GetID();
string time = GetTime();
string str = "insert into tb_SellGoods(sale_id,goods_name,emp_name,sale_goods_num," +
"sale_goods_time,sale_remark) values('" + id + "','" + text_name.Text.Trim() +
"','" + Form2.name + "'," + text_num.Text.Trim() + ",'" + time + "','" + "')";
cmd.CommandText = str;
cmd.ExecuteNonQuery();
MessageBox.Show("销售商品:" + text_name.Text + "\n数量:" + text_num.Text + "\n成功!");
//关闭
conn.Dispose();
cmd.Dispose();
da.Dispose();
ds.Dispose();
}
}
}
报表只做了两个,一个是用户报表,还有一个是进货报表。
用户报表:
进货报表:
这里我大概说一下报表的制作,首先插件的安装这是必须的
1、要有Microsoft SQL Server Data Tools,这个是在安装的时候勾选的,如果没有那就重新覆盖安装一遍
2、要有Microsoft Rdlc Report Designer for Visual Studio,如果没有,需要在扩展和更新中安装,安装好后需要重启vs
3、要有Microsoft.ReportingServices.ReportViewerControl.Winforms,如果没有,在VS中选择工具——Nuget包管理器——程序包管理器控制台
执行命令:* Install-Package Microsoft.ReportingServices.ReportViewerControl.WinForms -Pre *
然后在VS的工具——选择工具箱项——.NetFramework下,选择“浏览”,然后在工程根目录下的“\packages\Microsoft.ReportingServices.ReportViewerControl.Winforms.xx\lib\net40”目录下选择“Microsoft.ReportViewer.WinForms.dll”文件(.dll文件会有很多个,注意选择)
以上的插件还有包添加之后还是要重启一下软件,让包和插件重新加载,然后将控件添加到工具箱里方便使用。
在工具箱的随便哪个地方,右键单击,点击选择项就可以添加控件。
下面是关于报表的制作,我在制作的时候查阅了一些网上的相关资料,有很多推荐使用水晶报表的,我嫌麻烦直接就用的vs自带的报表了,下面简单记录一下报表的制作:
首先,添加数据集(dataset)
项目–添加新项,选择数据集,添加
我们在dataset里需要拖入数据库里做好的表
第二步,添加报表
项目–添加新项–reporting,添加
这里的报表要添加数据源,数据源就添加你刚刚做好的dataset,然后你可以在报表里画一些表,用于报表的展示
到这里报表基本完成了一半了,在reporting里你可以设置修改字体的大小和样式。
最后一步,添加winform窗体,将添加好的报表控件拖入窗体,写好代码,报表完成
注意:如果遇到报表展示出来是乱码,那很可能是你的reporting里面字体没设置好,重新设置字体,搞定!
我还做了帮助文档和关于说明
这里我用的magichelp软件写的帮助文档,也可以用微软的软件来写,都是免费的,但是这个用起来很方便。
还有一些简单的命令,我把它集成到这个菜单栏里,方便使用
代码:
```csharp
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsForms0929
{
partial class AboutBox1 : Form
{
public AboutBox1()
{
InitializeComponent();
this.Text = String.Format("关于 {0}", AssemblyTitle);
this.labelProductName.Text = AssemblyProduct;
this.labelVersion.Text = String.Format("版本 {0}", AssemblyVersion);
this.labelCopyright.Text = AssemblyCopyright;
this.labelCompanyName.Text = AssemblyCompany;
this.textBoxDescription.Text = AssemblyDescription;
}
#region 程序集特性访问器
public string AssemblyTitle
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if (attributes.Length > 0)
{
AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
if (titleAttribute.Title != "")
{
return titleAttribute.Title;
}
}
return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
}
}
public string AssemblyVersion
{
get
{
return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}
public string AssemblyDescription
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return ((AssemblyDescriptionAttribute)attributes[0]).Description;
}
}
public string AssemblyProduct
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return ((AssemblyProductAttribute)attributes[0]).Product;
}
}
public string AssemblyCopyright
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
}
}
public string AssemblyCompany
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return ((AssemblyCompanyAttribute)attributes[0]).Company;
}
}
#endregion
private void labelProductName_Click(object sender, EventArgs e)
{
}
private void textBoxDescription_TextChanged(object sender, EventArgs e)
{
}
private void AboutBox1_Load(object sender, EventArgs e)
{
}
}
}
经过此次项目实训,我深刻的了解到,完成一个通用功能齐全,并且没有系统报错的窗体软件是一件非常不容易的事。也清楚的理解了什么叫做用户体验,进一步的提升需要在平时生活中多积累,多观察。虽然项目已经完成,但是任然有许多不足的地方:比如超市会员没有细化,比如金卡会员】银卡会员和普通会员等,由于我的能力不足不得不放弃;还有查询功能还不够强大等,还不可以分类统计出结果。
还有其他的以后想起来再补充吧~~(●’◡’●)
以上!!