WinForm 数据库增、删、改查实例-洋葱先生-杨少通
发布日期:2021-10-03 07:58:47
浏览次数:1
分类:技术文章
本文共 2702 字,大约阅读时间需要 9 分钟。
前提:
创建数据库、插入相关数据,我的数据库表名为:Student_Info,插入数据如下:
实现功能:
①添加时如果id已存在,则弹出对话框:“您输入的id已存在”,成功后显示如图的提示信息
②删除是按照学号字段删除,删除成功弹出提示“您成功删除1条数据”
③修改是以学号为标准进行修改,修改成功弹出提示“您成功修改1条数据”
效果图如下:代码如下:
//实例化对象,用于开启程序和数据库之间的连接 SqlConnection conn; //窗体Load事件 private void _4_2_Load(object sender, EventArgs e) { //连接数据库 conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=WinFormSQL;Data Source=localhost"); //打开数据库连接 conn.Open(); } //“添加”按钮的Click事件 private void button1_Click(object sender, EventArgs e) { //实例化对象,用于对数据库发出指令 SqlCommand cmd = new SqlCommand(); //向数据库发出打开/关闭指令 cmd.Connection = conn; //向数据库发出查询指令 cmd.CommandText = "select count(*) from Student_Info where studentID=" + textBox1.Text; //ExecuteScalar()方法是从数据库中检索单个值,返回值是object类型 //用ExecuteScalar()是select中的“*”必须改为“count(*)” string r = cmd.ExecuteScalar().ToString(); //编号不存在就可以添加 if (r != "0") MessageBox.Show("编号已存在"); else { //“添加”指令字符串 string sql = "insert into Student_Info (studentID,studentName,studentClass) values('{0}','{1}','{2}')"; //执行指令字符串 cmd.CommandText = string.Format(sql, textBox1.Text, textBox2.Text, textBox3.Text); //ExecuteNonQuery()方法是返回受影响行数 int result = cmd.ExecuteNonQuery(); if (result == 1) MessageBox.Show("插入成功"); else MessageBox.Show("插入失败"); } } //“删除”按钮的Click事件 private void button2_Click(object sender, EventArgs e) { //“删除”指令字符串 string sql = "delete from Student_Info where studentID=" + textBox1.Text; //执行打开数据库连接指令和“删除”指令字符串 SqlCommand cmd = new SqlCommand(sql, conn); //ExecuteNonQuery()方法是返回受影响行数 int result = cmd.ExecuteNonQuery(); if (result == 1) MessageBox.Show("删除成功"); else MessageBox.Show("删除失败"); } //“修改”按钮的Click事件 private void button3_Click(object sender, EventArgs e) { //“修改”指令字符串 string sql = "update Student_Info set studentName='{0}',studentClass='{1}' where studentID=" + textBox1.Text; //实例化对象,用于对数据库发出指令 SqlCommand cmd = new SqlCommand(); //执行指令字符串 cmd.CommandText = string.Format(sql, textBox2.Text, textBox3.Text); //执行打开数据库指令 cmd.Connection = conn; //ExecuteNonQuery()方法是返回受影响行数 int result = cmd.ExecuteNonQuery(); if (result == 1) MessageBox.Show("修改成功"); else MessageBox.Show("修改失败"); }
代码均为原创,存在不足还请见谅!如有转载请注明来源: www.dreamload.cn/blog/?p=445&preview=true (洋葱先生)
转载地址:https://blog.csdn.net/Kindergarten_Sir/article/details/108588337 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月21日 00时41分25秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
java常用类 String面试题
2019-04-30
四线触摸屏原理
2019-04-30
C/C++如何返回一个数组/指针
2019-04-30
腾讯AI语音识别API踩坑记录
2019-04-30
java.net.BindException: 无法指定被请求的地址
2019-05-01
svn服务器安装
2019-05-01
spark 笔记1
2019-05-01
shell dirname basename
2019-05-01
未来已至,5G加持下的云游戏将走向何方?
2019-05-01
计算机网络 —— 网络层 1.
2019-05-01
Android 之 ContentProvider 与 ContentResolver
2019-05-01
【接口自动化】
2019-05-01
推荐一位川大零基础转行 Python 的人生勇士
2019-05-01
Python解惑之:True与False
2019-05-01
你要的微信小程序终于来了
2019-05-01
有了这些 Chrome 插件,效率提升10倍(建议收藏)
2019-05-01
只有1%的程序员搞懂过浮点数陷阱
2019-05-01
一名 Google 工程师的大数据处理经验
2019-05-01
命名难,难于上青天
2019-05-01
没钱没公司,怎么做一款付费产品
2019-05-01