c#窗体编辑个人简历_C#窗体简单增删改查

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Data.SqlClient;6 usingSystem.Drawing;7 usingSystem.Linq;8 usingSystem.Text;9 usingSystem.Threading.Tasks;10 usingSystem.Windows.Forms;11

12 namespacexueshengchengjiguanli13 {14 public partial classEdit : Form15 {16 //变量

17 public int Sno = -1;18 DataSet ds = newDataSet();19 publicEdit()20 {21 InitializeComponent();22 }23 //窗体加载

24 private void Edit_Load(objectsender, EventArgs e)25 {26 if (Sno == -1)//没有被选定的行数

27 {28

29 }30 else//修改

31 {32 GetInfo();33 this.btnSave.Text = "修改";34 }35 }36 //保存按钮

37 private void btnSave_Click(objectsender, EventArgs e)38 {39 if(CheckItem())40 {41 if (this.Sno == -1)//新增

42 {43 if(CheckSnoExit())44 {45 InsertDB();46 }47 }48 else//更新

49 {50 UpdateScore();51 }52 }53 }54 //非空验证

55 private boolCheckItem()56 {57 bool checkValue = true;58 if (this.textBox1.Text.Trim().Length == 0)59 {60 MessageBox.Show("学号不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);61 checkValue = false;62 this.textBox1.Text = " ";63 }64 returncheckValue;65 }66 //增加方法67 //查重

68 private boolCheckSnoExit()69 {70 bool exit = true;71 DBhelper dbhelper = newDBhelper();72 try

73 {74 StringBuilder sb = newStringBuilder();75 sb.AppendFormat("select * from Score where Sno='{0}'", textBox1.Text.Trim());76 SqlCommand cmd = newSqlCommand(sb.ToString(), dbhelper.Connection);77 dbhelper.OpenConnection();78 SqlDataReader reader =cmd.ExecuteReader();79 if(reader.Read())80 {81 MessageBox.Show("该学号已存在", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);82 exit = false;83 }84 reader.Close();85 }86 catch(Exception)87 {88 MessageBox.Show("数据库操作错误", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);89 }90 finally

91 {92 dbhelper.CloseConnection();93 }94 returnexit;95 }96 //执行增加

97 private voidInsertDB()98 {99 DBhelper helper = newDBhelper();100 try

101 {102 //SQL语句

103 StringBuilder sb = newStringBuilder();104 sb.AppendLine("insert into Score");105 sb.AppendFormat("values('{0}','{1}','{2}','{3}','{4}','{5}')", textBox1.Text.Trim(),textBox2.Text.Trim(),comboBox1.Text.Trim(),textBox4.Text.Trim(),textBox5.Text.Trim(),textBox6.Text.Trim());106 //执行工具

107 SqlCommand cmd = newSqlCommand(sb.ToString(), helper.Connection);108 //打开连接

109 helper.OpenConnection();110 //执行

111 int result =cmd.ExecuteNonQuery();112 if (result > 0)113 {114 MessageBox.Show("添加成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);115 this.Close();116 }117 }118 catch(Exception)119 {120 MessageBox.Show("添加数据库操作错误", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);121 }122 finally

123 {124 helper.CloseConnection();125 }126 }127

128 //通过ID查找类别

129 private voidGetInfo()130 {131 DBhelper dbhelper = newDBhelper();132 try

133 {134 //SQL语句

135 StringBuilder sb = newStringBuilder();136 sb.AppendLine("select Sno,Sname,Sclass,SChinese,SMath,SEnglish");137 sb.AppendLine("from Score");138 sb.AppendFormat("where Sno={0}", Sno);139 //执行工具

140 SqlCommand cmd = newSqlCommand(sb.ToString(), dbhelper.Connection);141 //打开连接

142 dbhelper.OpenConnection();143 //执行

144 SqlDataReader reader =cmd.ExecuteReader();145 if(reader.Read())146 {147 textBox1.Text = reader["Sno"].ToString();148 textBox2.Text = reader["Sname"].ToString();149 comboBox1.Text = reader["Sclass"].ToString();150 textBox4.Text = reader["SChinese"].ToString();151 textBox5.Text = reader["SMath"].ToString();152 textBox6.Text = reader["SEnglish"].ToString();153 }154 reader.Close();155 }156 catch(Exception)157 {158 MessageBox.Show("操作错误", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);159 }160 finally

161 {162 dbhelper.CloseConnection();163 }164

165 }166 //修改

167 private voidUpdateScore()168 {169 DBhelper helper = newDBhelper();170 try

171 {172 StringBuilder sql = newStringBuilder();173 //修改数据库语句

174 sql.AppendLine("update Score");175 sql.AppendFormat("set Sname='{0}',Sclass='{1}',SChinese='{2}',SMath='{3}',SEnglish='{4}'", textBox2.Text.Trim(), comboBox1.Text.Trim(), textBox4.Text.Trim(), textBox5.Text.Trim(), textBox6.Text.Trim());176 sql.AppendFormat("where Sno={0}", Sno);177 //执行工具

178 SqlCommand cmd = newSqlCommand(sql.ToString(), helper.Connection);179 //打开数据库连接

180 helper.OpenConnection();181 //执行

182 int result =cmd.ExecuteNonQuery();183 //判断

184 if (result == 1)185 {186

187 MessageBox.Show("修改成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);188 this.Close();189 }190 else

191 {192

193 MessageBox.Show("修改失败", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);194 }195 }196 catch(Exception)197 {198

199 MessageBox.Show("修改数据库操作错误", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);200 }201 finally

202 {203 helper.CloseConnection();204 }205

206 }207 //取消按钮

208 private void button2_Click(objectsender, EventArgs e)209 {210 this.Close();211 }212 }213 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值