using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace _03大项目 { public partial class Form1 : Form { //连接字符串 string str = "Data Source=.;Initial Catalog=MyDeskInfo;Integrated Security=True"; public Form1() { InitializeComponent(); } //窗体加载 private void Form1_Load(object sender, EventArgs e) { //加载餐桌 LoadDeskInfoByDelFlag(0); } /// <summary> /// 该方法是加载所有没有被删除的餐桌 /// </summary> /// <param name="p">删除标识 0---未删除,1---删除</param> private void LoadDeskInfoByDelFlag(int p) { List<DeskInfo> list = new List<DeskInfo>(); //通过连接字符串连接数据库 using (SqlConnection con = new SqlConnection(str)) { //拼接sql语句 string sql = "select DeskId,DeskName,DeskNamePinYin,DeskNum from DeskInfo where DeskDelFlag=" + p; //准备执行sql语句的对象 using (SqlCommand cmd = new SqlCommand(sql, con)) { con.Open();//打开数据库 //准备读数据 using (SqlDataReader reader = cmd.ExecuteReader()) { //判断是否有数据(有没有行) if (reader.HasRows) { //读取每一行 while (reader.Read()) { DeskInfo dk = new DeskInfo();//创建餐桌对象 dk.DeskId = Convert.ToInt32(reader["DeskId"]);//id dk.DeskName = reader["DeskName"].ToString();//名字 dk.DeskNamePinYin = reader["DeskNamePinYin"].ToString();//拼音 dk.DeskNum = reader["DeskNum"].ToString();//编号 list.Add(dk);//添加到集合中 }//end while }//end if }// end sqldatareader }//end using }//end using dgv.AutoGenerateColumns = false;//禁止自动生成列 dgv.DataSource = list;//绑定数据 到集合list,这样才有显示 dgv.SelectedRows[0].Selected = false;//禁止被选中 } //添加数据 private void btnOk_Click(object sender, EventArgs e) { int n = -1; //获取文本框的值 //连接数据库 using (SqlConnection con = new SqlConnection(str)) { string sql = string.Format("insert into DeskInfo(DeskName, DeskNamePinYin, DeskDelFlag, DeskNum) values(‘{0}‘,‘{1}‘,{2},‘{3}‘)", txtName.Text, txtPinYin.Text, 0, txtNum.Text); using (SqlCommand cmd = new SqlCommand(sql, con)) { con.Open();//打开数据库 n = cmd.ExecuteNonQuery(); } } string msg = n > 0 ? "操作成功" : "操作失败"; MessageBox.Show(msg); LoadDeskInfoByDelFlag(0);//刷新 } private void btnDelete_Click(object sender, EventArgs e) { //判断一下 是否选中行 if (dgv.SelectedRows.Count > 0) { int r = -1; //选中了 //该行数据的id string strId = dgv.SelectedRows[0].Cells[0].Value.ToString(); int id = Convert.ToInt32(strId);//转换一下 //update DeskInfo set DeskDelFlag=1 where DeskId= using (SqlConnection con = new SqlConnection(str)) { string sql = "update DeskInfo set DeskDelFlag=1 where DeskId=" + id; using (SqlCommand cmd = new SqlCommand(sql, con)) { con.Open(); r = cmd.ExecuteNonQuery(); }// end using }//end using string msg = r > 0 ? "操作成功" : "操作失败"; MessageBox.Show(msg); LoadDeskInfoByDelFlag(0);//刷新 } } private void dgv_CellClick(object sender, DataGridViewCellEventArgs e) { if (dgv.SelectedRows.Count>0) { //获取这条数据中的每个列中的值 //id name pinyin num labId.Text = dgv.SelectedRows[0].Cells[0].Value.ToString(); txtUName.Text = dgv.SelectedRows[0].Cells[1].Value.ToString(); txtUPinYin.Text = dgv.SelectedRows[0].Cells[2].Value.ToString(); txtUNum.Text = dgv.SelectedRows[0].Cells[3].Value.ToString(); } } private void btnUpdate_Click(object sender, EventArgs e) { if (dgv.SelectedRows.Count>0) { int r = -1; //修改 using (SqlConnection con=new SqlConnection(str)) { string sql =string.Format("update DeskInfo set DeskName=‘{0}‘,DeskNamePinYin=‘{1}‘,DeskNum=‘{2}‘ where DeskId={3}",txtUName.Text,txtUPinYin.Text,txtUNum.Text,Convert.ToInt32(labId.Text)); using (SqlCommand cmd=new SqlCommand(sql,con)) { con.Open(); r= cmd.ExecuteNonQuery(); }// end using }// end using MessageBox.Show(r>0?"操作成功":"操作失败"); LoadDeskInfoByDelFlag(0); } } //显示有多少条数据 private void button1_Click(object sender, EventArgs e) { //不要这个 //MessageBox.Show(dgv.SelectedRows.Count); //练习 object obj; using (SqlConnection con=new SqlConnection(str)) { string sql = "select count(*) from DeskInfo where DeskDelFlag=0"; using (SqlCommand cmd=new SqlCommand(sql,con)) { con.Open(); obj= cmd.ExecuteScalar(); } } MessageBox.Show(obj.ToString()); } private void btnGc_Click(object sender, EventArgs e) { //查询的是被删除的数据 LoadDeskInfoByDelFlag(1); } private void btnCDDelete_Click(object sender, EventArgs e) { string id = dgv.SelectedRows[0].Cells[0].Value.ToString(); //还是 更新 改标识 5 恢复 数据需要再交钱 100 超邪恶的方法 //稍稍的修改一下标识 // 如果有一天 我邪恶了 请记住 我曾纯洁过 int r = -1; //修改 using (SqlConnection con = new SqlConnection(str)) { string sql = "update DeskInfo set DeskDelFlag=5"; using (SqlCommand cmd = new SqlCommand(sql, con)) { con.Open(); r = cmd.ExecuteNonQuery(); }// end using }// end using MessageBox.Show(r > 0 ? "操作成功" : "操作失败"); } } }
时间: 2024-12-14 02:49:38