1.封装DeskInfo类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 项目1
{
public class DeskInfo
{
private int deskId;
private string deskName;
private string deskNamePinYin;
private string deskNum;
/// <summary>
/// 餐桌Id
/// </summary>
public int DeskId
{
get
{
return deskId;
}
set
{
deskId = value;
}
}
/// <summary>
/// 餐桌名字
/// </summary>
public string DeskName
{
get
{
return deskName;
}
set
{
deskName = value;
}
}
/// <summary>
/// 餐桌名字拼音
/// </summary>
public string DeskNamePinYin
{
get
{
return deskNamePinYin;
}
set
{
deskNamePinYin = value;
}
}
/// <summary>
/// 餐桌编号
/// </summary>
public string DeskNum
{
get
{
return deskNum;
}
set
{
deskNum = value;
}
}
}
}
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;
using System.Data.SqlClient;
namespace 项目1
{
public partial class 登录界面 : Form
{
public 登录界面()
{
InitializeComponent();
}
private void btLog_Click(object sender, EventArgs e)
{
try
{
string str = "Data Source=LENOVO-PC;Initial Catalog=MyDatabase;Integrated Security=True";
using (SqlConnection sco = new SqlConnection(str))
{
//防止注入漏洞
string sql = "select *from Users where logName=@name";
using (SqlCommand scd = new SqlCommand(sql, sco))
{
sco.Open();
scd.Parameters.AddWithValue("@name",textLogName.Text);
using (SqlDataReader sdr = scd.ExecuteReader())
{
if (sdr.HasRows)
{
while (sdr.Read())
{
string pwd = sdr["logPassWord"].ToString();
if (pwd == textLogPwd.Text)
{
MessageBox.Show("登录成功!");
this.Hide();/****************测试*******************/
主界面 form = new 主界面();
form.Show();
}
else
{
MessageBox.Show("密码不正确,登录失败!");
textLogName.Clear();
textLogPwd.Clear();
textLogPwd.Focus();
}
}
}
else
{
MessageBox.Show("用户名输入错误或者不存在,请先注册!");
textLogName.Clear();
textLogPwd.Clear();
textLogName.Focus();
}
}
sco.Close();
}
}
}
catch { }
}
private void btLog_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btLog.PerformClick();
}
}
private void 登录界面_Load(object sender, EventArgs e)
{
textLogName.Focus();
}
private void btZhuCe_Click(object sender, EventArgs e)
{
this.Hide();
注册界面 form = new 注册界面();
form.Show();
}
private void 登录界面_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
if (DialogResult.Yes == MessageBox.Show("是否退出登录?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
{
Environment.Exit(0);
}
else
{
e.Cancel = true;
}
}
catch { }
}
}
}
3.注册界面
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.SqlClient;
namespace 项目1
{
public partial class 注册界面 : Form
{
public 注册界面()
{
InitializeComponent();
}
private void btcertain_Click(object sender, EventArgs e)
{
try
{
int n = -1;
string str = "Data Source=LENOVO-PC;Initial Catalog=MyDatabase;Integrated Security=True";
if (!(textzhucepwd.Text.Equals(textzhucecertainpwd.Text)))
{
MessageBox.Show("两次输入密码不一致!");
textzhucepwd.Clear();
textzhucecertainpwd.Clear();
textzhucepwd.Focus();
}
else
{
using (SqlConnection sco = new SqlConnection(str))
{
//string sql = string.Format("insert into Users(logName, logPassWord)values('{0}','{1}')", textzhucename.Text, textzhucepwd.Text);
string sql = "insert into Users(logName, logPassWord)values(@name,@pwd)";
using (SqlCommand scd = new SqlCommand(sql, sco))
{
sco.Open();
scd.Parameters.AddWithValue("@name", textzhucename.Text);
scd.Parameters.AddWithValue("@pwd",textzhucepwd.Text);
n = scd.ExecuteNonQuery();
}
sco.Close();
}
if (n > 0)
{
MessageBox.Show("注册成功!");
this.Hide();
登录界面 form = new 登录界面();
form.Show();
}
else
{
MessageBox.Show("注册失败!");
}
}
}
catch (Exception ex)
{
MessageBox.Show("注册失败,用户已经存在!"+ex.Message);
textzhucename.Clear();
textzhucepwd.Clear();
textzhucecertainpwd.Clear();
textzhucename.Focus();
}
}
private void btcancel_Click(object sender, EventArgs e)
{
this.Hide();
登录界面 form = new 登录界面();
form.Show();
}
private void 注册界面_FormClosing(object sender, FormClosingEventArgs e)
{
if (DialogResult.Yes == MessageBox.Show("是否取消注册?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
{
Application.Exit();
}
else
{
e.Cancel = true;
}
}
}
}
4.主界面
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.SqlClient;
namespace 项目1
{
public partial class 主界面 : Form
{
string str = "Data Source=LENOVO-PC;Initial Catalog=MyDatabase;User ID=sa;Password=pingxing9132";
public 主界面()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LoadDeskInfoByFlag(0);//判断是否删除,查询没删除的行,0---未删除,1----删除
panel1.Visible = false;
}
private void LoadDeskInfoByFlag(int p)
{
try
{
List<DeskInfo> list = new List<DeskInfo>();//放置数据与DataGuidview连接起来
using (SqlConnection sco = new SqlConnection(str))//连接数据库
{
string sql = "select DeskId,DeskName,DeskNamePinYin,DeskNum from DeskInfo where DeskDelFlag=" + p;//拼接sql语句
using (SqlCommand scd = new SqlCommand(sql, sco))//准备执行sql语句
{
sco.Open();//打开数据库
using (SqlDataReader reader = scd.ExecuteReader())//读取数据,一行一行读
{
if (reader.HasRows)//判断是否还有行
{
while (reader.Read())//读取数据,返回值为bool值。如果还有行返回true,如果没有行则返回false;
{
//将关系转化为对象
DeskInfo dk = new DeskInfo();
dk.DeskId = Convert.ToInt32(reader["DeskId"]);
dk.DeskName = reader["DeskName"].ToString();
dk.DeskNamePinYin = reader["DeskNamePinYin"].ToString();
dk.DeskNum = reader["DeskNum"].ToString();
list.Add(dk);
}
}
}
sco.Close();
}
}
dgv.AutoGenerateColumns = false;//禁止自动生成列
dgv.DataSource = list;//绑定数据
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 添加数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btadd_Click(object sender, EventArgs e)
{
int n = -1;
try
{
using (SqlConnection sco = new SqlConnection(str))
{
sco.Open();
string sql = string.Format("insert into DeskInfo(DeskName, DeskNamePinYin, DeskDelFlag, DeskNum) values('{0}','{1}','{2}','{3}')", textname.Text, textpinyin.Text, 0, textnum.Text);
using (SqlCommand scd = new SqlCommand(sql, sco))
{
n = scd.ExecuteNonQuery();
}
sco.Close();
}
if (n > 0)
{
MessageBox.Show("添加成功!");
LoadDeskInfoByFlag(0);
textname.Clear();
textpinyin.Clear();
textnum.Clear();
}
else
{
MessageBox.Show("添加失败!");
}
}
catch (Exception ex)
{
DialogResult r= MessageBox.Show("已经存在" + textname.Text + "这个名字,请重新修改!","信息提示",MessageBoxButtons.OK);
if (r == DialogResult.OK)
{
textname.Clear();
textname.Focus();
}
}
}
private void btcancel_Click(object sender, EventArgs e)
{
textname.Clear();
textpinyin.Clear();
textnum.Clear();
textname.Focus();
}
/// <summary>
/// 删除数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btupdate_Click(object sender, EventArgs e)
{
int n = -1;
try
{
if (dgv.SelectedRows.Count > 0)
{
string strId = dgv.SelectedRows[0].Cells[0].Value.ToString();
int id = Convert.ToInt32(strId);
using (SqlConnection sco = new SqlConnection(str))
{
string sql = "update DeskInfo set DeskDelFlag=1 where DeskId=" + id;
using (SqlCommand scd = new SqlCommand(sql, sco))
{
sco.Open();
n = scd.ExecuteNonQuery();
}
sco.Close();
}
if (n > 0)
{
MessageBox.Show("删除成功!");
LoadDeskInfoByFlag(0);
textuname.Clear();
textunum.Clear();
textupinyin.Clear();
}
else
{
MessageBox.Show("删除失败!");
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 修改数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btucertain_Click(object sender, EventArgs e)
{
int n = -1;
try
{
if (dgv.SelectedRows.Count > 0)
{
using (SqlConnection sco = new SqlConnection(str))
{
string sql = string.Format("update DeskInfo set DeskName='{0}',DeskNamePinYin='{1}',DeskNum='{2}' where DeskId='{3}'", textuname.Text, textupinyin.Text, textunum.Text, lbunum.Text);
using (SqlCommand scd = new SqlCommand(sql, sco))
{
sco.Open();
n = scd.ExecuteNonQuery();
}
sco.Close();
}
if (n > 0)
{
LoadDeskInfoByFlag(0);
DialogResult r = MessageBox.Show("修改成功!", "信息提示", MessageBoxButtons.OK);
if (r == DialogResult.OK)
{
textuname.Clear();
textunum.Clear();
textupinyin.Clear();
}
}
else
{
MessageBox.Show("修改失败!");
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 选中哪行事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dgv.SelectedRows.Count > 0)
{
lbunum.Text = dgv.SelectedRows[0].Cells[0].Value.ToString();
textuname.Text = dgv.SelectedRows[0].Cells[1].Value.ToString();
textupinyin.Text = dgv.SelectedRows[0].Cells[2].Value.ToString();
textunum.Text = dgv.SelectedRows[0].Cells[3].Value.ToString();
}
}
/// <summary>
/// 显示多少条数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
object n;
using (SqlConnection sco=new SqlConnection(str))
{
string sql = "select Count(*) from DeskInfo where DeskDelflag=0";
using (SqlCommand scd=new SqlCommand(sql, sco))
{
sco.Open();
n = scd.ExecuteScalar();
}
sco.Close();
}
MessageBox.Show(n.ToString());
}
/// <summary>
/// 回收站
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void bthuishouzhan_Click(object sender, EventArgs e)
{
LoadDeskInfoByFlag(1);
}
/// <summary>
/// 从回收站返回
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btback_Click(object sender, EventArgs e)
{
LoadDeskInfoByFlag(0);
}
/// <summary>
/// 彻底删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btdelete_Click(object sender, EventArgs e)
{
int n = -1;
if (dgv.SelectedRows.Count>0)
{
string strId = dgv.SelectedRows[0].Cells[0].Value.ToString();
int id = Convert.ToInt32(strId);
using(SqlConnection sco=new SqlConnection(str))
{
string sql =" update DeskInfo set DeskDelFlag = 2 where DeskId = " + id;
using (SqlCommand scd=new SqlCommand(sql, sco))
{
sco.Open();
n = scd.ExecuteNonQuery();
}
sco.Close();
}
if (n>0)
{
DialogResult r = MessageBox.Show("确认删除?","信息提示",MessageBoxButtons.OKCancel);
if (r ==DialogResult.OK)
{
LoadDeskInfoByFlag(1);
textuname.Clear();
textunum.Clear();
textupinyin.Clear();
}
else
{
using (SqlConnection sco = new SqlConnection(str))
{
string sql1 = " update DeskInfo set DeskDelFlag = 1 where DeskId = " + id;
using (SqlCommand scd = new SqlCommand(sql1, sco))
{
sco.Open();
n = scd.ExecuteNonQuery();
}
sco.Close();
}
LoadDeskInfoByFlag(1);
textuname.Clear();
textunum.Clear();
textupinyin.Clear();
}
}
else
{
MessageBox.Show("彻底删除失败!");
}
}
}
/// <summary>
/// 数据恢复
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btrecover_Click(object sender, EventArgs e)
{
int n = -1;
if (dgv.SelectedRows.Count > 0)
{
string strId = lbunum.Text;
int id = Convert.ToInt32(strId);
using (SqlConnection sco=new SqlConnection(str))
{
string sql = "update DeskInfo set DeskDelFlag=0 where DeskId="+id;
using (SqlCommand scd=new SqlCommand(sql, sco))
{
sco.Open();
n = scd.ExecuteNonQuery();
}
sco.Close();
}
if (n>0)
{
MessageBox.Show("数据恢复成功!");
LoadDeskInfoByFlag(1);
textuname.Clear();
textunum.Clear();
textupinyin.Clear();
}
else
{
MessageBox.Show("数据恢复失败!");
}
}
}
private void button1_Click_1(object sender, EventArgs e)
{
panel1.Visible = false;
}
private void 功能菜单ToolStripMenuItem_Click(object sender, EventArgs e)
{
panel1.Visible = true;
}
private void 回收站ToolStripMenuItem_Click(object sender, EventArgs e)
{
LoadDeskInfoByFlag(1);
}
private void Form1_Click(object sender, EventArgs e)
{
panel1.Visible = false;
}
private void 主界面_FormClosing(object sender, FormClosingEventArgs e)
{
if (DialogResult.Yes == MessageBox.Show("是否退出系统?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
{
Application.Exit();
}
else
{
e.Cancel = true;
}
}
}
}
5.Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 项目1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new 登录界面());
}
}
}