说明(2017-5-28 11:35:39):
1. 需要注意的地方很多
2. 首先是连接字符串str要直接写在类里面,不然每个按钮里面都要写一遍。
3. 查询用到sqlDataReader等三个方法,其他增删改只用到sqlCommond和sqlConnection。
4. sqlConnection记得要open!
5. 其他的记不起来了,总之要经常练习!
运行结果:
代码:
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 namespace _03大项目查找 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 20 } 21 string str = "Data Source=.; Initial Catalog=mysql; Integrated Security=True;"; 22 23 private void Form1_Load(object sender, EventArgs e) 24 { 25 26 loadStudent(0); 27 } 28 /// <summary> 29 /// 加载学生信息 30 /// </summary> 31 /// <param name="p">p是删除标识,0删除,1未删除</param> 32 public void loadStudent(int p) 33 { 34 List<Student> list = new List<Student>(); 35 36 using (SqlConnection con = new SqlConnection(str)) 37 { 38 con.Open(); 39 string sql = "select id, name, chinese, math, flag from myclass where flag = " + p; 40 using (SqlCommand cmd = new SqlCommand(sql, con)) 41 { 42 using (SqlDataReader reader = cmd.ExecuteReader()) 43 { 44 if (reader.HasRows) 45 { 46 while (reader.Read()) 47 { 48 Student st = new Student(); 49 st.Id = Convert.ToInt32(reader["id"]); 50 st.Name = Convert.ToString(reader["name"]); 51 st.Chinese = Convert.ToString(reader["chinese"]); 52 st.Math = Convert.ToString(reader["math"]); 53 st.Flag = Convert.ToInt32(reader["flag"]); 54 list.Add(st); 55 } 56 } 57 } 58 } 59 } 60 61 dgv.DataSource = list; 62 dgv.SelectedRows[0].Selected = false; 63 } 64 /// <summary> 65 /// 添加学生信息 66 /// </summary> 67 /// <param name="sender"></param> 68 /// <param name="e"></param> 69 private void button1_Click(object sender, EventArgs e) 70 { 71 int n = -999; 72 string name = txtName.Text; 73 string chinese = txtChinese.Text; 74 string math = txtMath.Text; 75 string sql = string.Format("insert into myclass(name,chinese,math,flag) values('{0}','{1}','{2}',0)", name, chinese, math); 76 using (SqlConnection con = new SqlConnection(str)) 77 { 78 using (SqlCommand cmd = new SqlCommand(sql, con)) 79 { 80 con.Open(); 81 n = cmd.ExecuteNonQuery(); 82 MessageBox.Show(n > 0 ? "操作成功!" : "操作失败!"); 83 } 84 } 85 loadStudent(0); 86 87 //MessageBox.Show("123"); 88 } 89 /// <summary> 90 /// 删除学生 91 /// </summary> 92 /// <param name="sender"></param> 93 /// <param name="e"></param> 94 private void btnDel_Click(object sender, EventArgs e) 95 { 96 int n = -999; 97 string id = dgv.SelectedRows[0].Cells[0].Value.ToString(); 98 string name = dgv.SelectedRows[0].Cells[1].ToString(); 99 string chinese = dgv.SelectedRows[0].Cells[2].ToString(); 100 101 string sql = string.Format("update myclass set flag = 1 where id ={0}", id); 102 using (SqlConnection con = new SqlConnection(str)) 103 { 104 using (SqlCommand cmd = new SqlCommand(sql, con)) 105 { 106 con.Open(); 107 n = cmd.ExecuteNonQuery(); 108 MessageBox.Show(n > 0 ? "操作成功!" : "操作失败!s"); 109 } 110 } 111 loadStudent(0); 112 } 113 /// <summary> 114 /// 选中一行,显示在修改框中 115 /// </summary> 116 /// <param name="sender"></param> 117 /// <param name="e"></param> 118 private void dgv_CellClick(object sender, DataGridViewCellEventArgs e) 119 { 120 lbId.Text = dgv.SelectedRows[0].Cells[0].Value.ToString(); 121 txtUname.Text = dgv.SelectedRows[0].Cells[1].Value.ToString(); 122 txtUchinese.Text = dgv.SelectedRows[0].Cells[2].Value.ToString(); 123 txtUmath.Text = dgv.SelectedRows[0].Cells[3].Value.ToString(); 124 125 } 126 127 private void btnUpdate_Click(object sender, EventArgs e) 128 { 129 int n = -999; 130 if (dgv.SelectedRows.Count>0) 131 { 132 string sql = string.Format("update myclass set name = {0}, chinese = {1}, math = {2} where id = {3}", txtUname.Text,txtUchinese.Text,txtUmath.Text,lbId.Text); 133 using (SqlConnection con = new SqlConnection(str)) 134 { 135 using (SqlCommand cmd = new SqlCommand(sql,con)) 136 { 137 con.Open(); 138 n = cmd.ExecuteNonQuery(); 139 MessageBox.Show(n>0?"操作成功!":"操作失败!"); 140 } 141 } 142 } 143 else 144 { 145 MessageBox.Show("请输入数据!"); 146 } 147 loadStudent(0); 148 } 149 } 150 }