商品打折管理信息系统 使用C#开发,使用软件是visual studio 2017
程序员文章站
2022-05-13 20:23:52
...
页面做的比较简单。
下图左边程序主界面,右边是各个页面的代码。
主程序入口代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace sale
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login());
// Application.Run(new MainForm());//测试用
}
}
}
下面是主页面代码。
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace sale
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)//修改
{
Change form = new Change();
form.Show();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)//添加
{
Add form = new Add();//新建一个对象,打开添加窗口
form.Show();
}
private void button4_Click(object sender, EventArgs e)//删除 不会写直接删除,只好和其他一样,多做一个窗口
{
Delete form = new Delete();//打开删除的页面
form.Show();
}
private void button5_Click(object sender, EventArgs e)//查询
{
if (this.textBox1.Text.Trim() == "")
{
MessageBox.Show("请输入要查询的商品名称", "提示");
}
else{
string str = @"Data Source=zky\sqlexpress;Initial Catalog=sale;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlCommand cmd = new SqlCommand();//绑定SqlConnection对象
cmd.CommandText = "Select* from good where name_good='" + this.textBox1.Text.Trim() + "'";//
cmd.Connection = conn;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();//数据集
sda.Fill(ds);
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = ds.Tables[0].ToString();//依次赋值
this.dataGridView1.AutoGenerateColumns =true;
dataGridView1.ReadOnly = true;//不允许直接在列表上编辑
dataGridView1.Columns[2].Visible = false;//第2列不出现在表中
dataGridView1.Columns[4].Visible = false;//第4列不出现在表中
dataGridView1.Columns[5].Visible = false;//第5列不出现在表中
conn.Close();
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)//主显示框
{
}
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void MainForm_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“saleDataSet.good”中。您可以根据需要移动或删除它。
this.goodTableAdapter.Fill(this.saleDataSet.good);
}
private void button6_Click(object sender, EventArgs e)//显示所有商品的信息
{
string str = @"Data Source=zky\sqlexpress;Initial Catalog=sale;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlDataAdapter sqlDap = new SqlDataAdapter("Select Id_good,name_good,price,sale from good", conn);
DataSet dds = new DataSet();//数据集
sqlDap.Fill(dds);
DataTable _table = dds.Tables[0];
int count = _table.Rows.Count;
dataGridView1.DataSource = _table;
this.dataGridView1.AutoGenerateColumns = true;
conn.Close();
conn.Close();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)//商品名称
{
}
}
}
然后是登陆窗口
代码:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Drawing;
namespace sale
{
public partial class Login : Form
{
public Login()
{
InitializeComponent();
}
public new FormStartPosition StartPosition { get; set; }
private void button1_Click(object sender, EventArgs e)//注册
{
this.Hide();
Register f5 = new Register();
f5.ShowDialog();
}//打开注册页面
private void label2_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)//登陆
{
//数据库连接字符串(引号中的字符串为之前复制的那段字符)
//Data source=服务器名,Initial catalog=数据库名,User Id=sqlserver连接名,
//Password=数据库连接密码,integrated Security=True
string str = @"Data Source=zky\sqlexpress;Initial Catalog=sale;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
conn.Open();
string selectsql = "Select * from login where username = '" + textBox1.Text + "' and password='" + textBox2.Text + "'";
SqlCommand cmd = new SqlCommand(selectsql, conn); //SqlCommand对象允许你指定在数据库上执行的操作的类型。
cmd.CommandType = CommandType.Text;//这就是告诉cmd执行的sql是你赋给CommandText的值里写出的sql语句,如果是存储过程的话cmd.CommandType = CommandType.StoredProcedure;
//然后CommandText='存储过程的名字',这就是告诉cmd执行的是存储过程,
//存储过程的名字就是CommandText的值
SqlDataReader sdr;
sdr = cmd.ExecuteReader();
if (sdr.Read())//显示登录界面;
{
MessageBox.Show("登陆成功", "提示");
MainForm form= new MainForm();//登陆成功显示主界面
// MainForm.StartPosition = FormStartPosition.CenterScreen;
form.Show();
}
else
{
MessageBox.Show("登陆失败!请检查用户名或者密码重新登陆!", "提示");//弹出窗口提示用户
return;
}
conn.Close();
}
private void Form4_Load(object sender, EventArgs e)
{
}
private void label3_Click_1(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
注册:
代码:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace sale
{
public partial class Register : Form
{
public Register()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string str = @"Data Source=zky\sqlexpress;Initial Catalog=sale;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
conn.Open();
string selectsql = "select username from login";//验证用户名是否重复
SqlCommand com = new SqlCommand(selectsql, conn);
SqlDataReader reader;
reader = com.ExecuteReader();
while(reader.Read())
if (textBox1.Text == reader["username"].ToString().Trim())
{
MessageBox.Show("用户名已经存在,请重新输入!", "提示");//弹出窗口提示用户
return;
}
else if (textBox1.Text != reader["username"].ToString().Trim())//注册成功显示登陆页面
{
conn.Close();
conn.Open();
string insertsql = "insert into login values('" + textBox1.Text + "','" + textBox2.Text + "') ";
SqlCommand cmd = new SqlCommand(insertsql, conn);// 定义cmd对象并连接数据库
cmd.CommandType = CommandType.Text;//存储过程
cmd.ExecuteNonQuery();
MessageBox.Show("恭喜您,注册成功!", "提示");
this.Close();//关闭注册窗口
Login form = new Login(); //打开登陆窗口
form.Show();
return;
}
conn.Close();// 关闭对象并释放所占内存空间
conn.Dispose();
}
private void Form5_Load(object sender, EventArgs e)
{
}
}
}
添加页面:
代码:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace sale
{
public partial class Add : Form
{
public Add()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string code;
code = Convert.ToString(this.textBox1.Text);
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"'", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
code = regex.Replace(code, " ");
string str = @"Data Source=zky\sqlexpress;Initial Catalog=sale;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
conn.Open();
string sql = "select count(*) from good where Id_good=('" + code + "')";
SqlCommand cmd1 = new SqlCommand(sql, conn);
int count = (int)cmd1.ExecuteScalar();
if (count > 1)
MessageBox.Show("商品已经!", "提示");
else
{
SqlCommand cmd = conn.CreateCommand();//值得注意
cmd.CommandText = "insert into good(Id_good,name_good,sale,price,Id_supplier,Id_shelf,Id_kind) values (@Id_good,@sale,@name_good,@price,@Id_supplier,@Id_shelf,@Id_kind)";//注意逗号用英文的
cmd.Parameters.Add("@Id_good", SqlDbType.NVarChar);//
cmd.Parameters.Add("@sale", SqlDbType.VarChar, 20);
cmd.Parameters.Add("@price", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@name_good", SqlDbType.VarChar, 20);
cmd.Parameters.Add("@Id_supplier", SqlDbType.NVarChar);
cmd.Parameters.Add("@Id_kind", SqlDbType.NVarChar);
cmd.Parameters.Add("@Id_shelf", SqlDbType.NVarChar);
cmd.Parameters["@Id_good"].Value = textBox1.Text.Trim();
cmd.Parameters["@sale"].Value = textBox2.Text.Trim();
cmd.Parameters["@price"].Value = textBox3.Text.Trim();
cmd.Parameters["@name_good"].Value = textBox4.Text.Trim();
cmd.Parameters["@Id_supplier"].Value = textBox5.Text.Trim();
cmd.Parameters["@Id_kind"].Value = textBox6.Text.Trim();
cmd.Parameters["@Id_shelf"].Value = textBox7.Text.Trim();
try
{
conn.Open();
if (cmd.ExecuteNonQuery() == 1)
MessageBox.Show("商品写入成功!", "提示");
}
catch
{
MessageBox.Show("商品已存在,写入失败!", "提示");
this.Close();
}
//cmd.ExecuteNonQuery();//增加了一条新纪录,所以返回1
/*if (cmd.ExecuteNonQuery() > 0)
MessageBox.Show( "数据写入成功!", "提示");
else
MessageBox.Show("数据写入错误!", "提示");
*/
conn.Close();
this.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)//供货商
{
}
private void textBox7_TextChanged(object sender, EventArgs e)//货架
{
}
private void textBox6_TextChanged(object sender, EventArgs e)//kind 类型
{
}
private void label5_Click(object sender, EventArgs e)
{
}
}
}
删除:
代码:
using System;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;
namespace sale
{
public partial class Delete : Form
{
public Delete()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string str = @"Data Source=zky\sqlexpress;Initial Catalog=sale;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlCommand cmd = new SqlCommand("delete from good where Id_good='" + this.textBox1.Text.Trim() + "'");
cmd.Connection = conn;
cmd.Parameters.Add("@Id_good", SqlDbType.NVarChar);
cmd.Parameters["@Id_good"].Value = textBox1.Text.Trim();
cmd.ExecuteNonQuery();
conn.Close();
this.Close();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Delete_Load(object sender, EventArgs e)
{
}
}
}
修改:
代码:
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace sale
{
public partial class Change : Form
{
public Change()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox2.Text.Trim() == "" || textBox3.Text.Trim() == "" )
{
MessageBox.Show("请输入完整信息!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
string str = @"Data Source=zky\sqlexpress;Initial Catalog=sale;Integrated Security=True";
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "update good set aaa@qq.com where aaa@qq.com_good";
cmd.Connection = conn;
cmd.Parameters.Add("@Id_good", SqlDbType.NVarChar);
cmd.Parameters.Add("@sale", SqlDbType.VarChar, 50);
cmd.Parameters["@Id_good"].Value = textBox2.Text.Trim();
cmd.Parameters["@sale"].Value = textBox3.Text.Trim();
cmd.ExecuteNonQuery();
conn.Close();
this.Close();
}
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
}
}
上一篇: Protobuf-net
下一篇: 基于C#窗体的学生成绩/信息管理系统