1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.Data.SqlClient; 11 12 13 namespace 餐厅复习 14 { 15 public partial class Form1 : Form 16 { 17 public Form1() 18 { 19 InitializeComponent(); 20 } 21 22 private void Form1_Load(object sender, EventArgs e) 23 { 24 LoadDeskInfoByDelId(0); 25 26 } 27 string str = "Data Source=.;Initial Catalog=mysql;Integrated Security=True"; 28 private void LoadDeskInfoByDelId(int p) 29 { 30 //查询 31 List<DeskInfo> list = new List<DeskInfo>(); 32 33 string sql = "select * from DeskInfo where DeskDelFlag =" + p; 34 SqlConnection con = new SqlConnection(str); 35 con.Open(); 36 SqlCommand cmd = new SqlCommand(sql, con); 37 SqlDataReader reader = cmd.ExecuteReader(); 38 if (reader.HasRows) 39 { 40 while (reader.Read()) 41 { 42 list.Add(new DeskInfo() 43 { 44 DeskId = Convert.ToInt32(reader["DeskId"]), 45 DeskName = reader["DeskName"].ToString(), 46 DeskNamePinYin = reader["DeskNamePinYin"].ToString(), 47 DeskNum = reader["DeskNum"].ToString() 48 }); 49 } 50 } 51 dgv.DataSource = list; 52 dgv.Rows[0].Selected = false; 53 dgv.AutoGenerateColumns = false; 54 55 con.Dispose(); 56 cmd.Dispose(); 57 reader.Dispose(); 58 } 59 60 private void btnOK_Click(object sender, EventArgs e) 61 { 62 //添加 63 string sql = string.Format("insert into DeskInfo(DeskName,DeskNamePinYin,DeskDelFlag,DeskNum) values (‘{0}‘,‘{1}‘,0,‘{2}‘)",txtName.Text,txtPinYin.Text,txtNum.Text); 64 SqlConnection con = new SqlConnection(str); 65 con.Open(); 66 SqlCommand cmd = new SqlCommand(sql,con); 67 cmd.ExecuteNonQuery(); 68 LoadDeskInfoByDelId(0); 69 con.Dispose(); 70 cmd.Dispose(); 71 } 72 73 private void btnDel_Click(object sender, EventArgs e) 74 { 75 //删除 76 int delId = Convert.ToInt32(dgv.SelectedRows[0].Cells[0].Value); 77 string sql = "update DeskInfo set DeskDelFlag = 1 where DeskId = " + delId; 78 SqlConnection con = new SqlConnection(str); 79 con.Open(); 80 SqlCommand cmd = new SqlCommand(sql,con); 81 cmd.ExecuteNonQuery(); 82 LoadDeskInfoByDelId(0); 83 84 85 86 con.Dispose(); 87 cmd.Dispose(); 88 } 89 90 91 92 } 93 }
时间: 2024-09-30 16:35:55