-
vb.net 教程 8-3 数据库操作10-2
版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
接上一节,下面开始正式进行修改:
使用sql的update语句:
Private Sub btnEdit1_Click(sender As Object, e As EventArgs) Handles btnEdit1.Click
Dim id As Integer = cbEmployee.SelectedIndex + 1
Dim odcommand As New OleDbCommand()
odcommand.CommandText = "update 雇员 set 职务='" & txtDuties.Text &
"',尊称='" & txtAppellation.Text &
"',出生日期='" & dtpBirthday.Value &
"',雇用日期='" & dtpEmployDay.Value &
"',国家='" & txtCountry.Text &
"',城市='" & txtCity.Text &
"',地区='" & txtArea.Text &
"',地址='" & txtAddress.Text &
"',邮政编码='" & txtZipcode.Text &
"',家庭电话='" & txtPhone.Text &
"' where 雇员ID=" & id
odcommand.Connection = odc
Dim updateCount As Integer
Try
updateCount = odcommand.ExecuteNonQuery()
MessageBox.Show("修改成功 " & updateCount & " 条")
Catch ex As Exception
MessageBox.Show("修改失败:" & ex.Message)
End Try
End Sub
odcommand.ExecuteNonQuery返回修改的条数。
也可以用加参数的方法:
Private Sub btnEdit2_Click(sender As Object, e As EventArgs) Handles btnEdit2.Click
Dim id As Integer = cbEmployee.SelectedIndex + 1
Dim odcommand As New OleDbCommand()
odcommand.CommandText = "update 雇员 set 职务=@Duties,
尊称=@Appellation,
出生日期=@Birthday,
雇用日期=@EmployDay,
国家=@Country,
城市=@City,
地区=@Area,
地址=@Address,
邮政编码=@Zipcode,
家庭电话=@Phone
where 雇员ID=" & id
odcommand.Connection = odc
odcommand.Parameters.Add("@Duties", OleDbType.VarChar)
odcommand.Parameters("@Duties").Value = txtDuties.Text
odcommand.Parameters.Add("@Appellation", OleDbType.VarChar)
odcommand.Parameters("@Appellation").Value = txtAppellation.Text
odcommand.Parameters.Add("@Birthday", OleDbType.Date)
odcommand.Parameters("@Birthday").Value = dtpBirthday.Value
odcommand.Parameters.Add("@EmployDay", OleDbType.Date)
odcommand.Parameters("@EmployDay").Value = dtpEmployDay.Value
odcommand.Parameters.Add("@Country", OleDbType.VarChar)
odcommand.Parameters("@Country").Value = txtCountry.Text
odcommand.Parameters.Add("@City", OleDbType.VarChar)
odcommand.Parameters("@City").Value = txtCity.Text
odcommand.Parameters.Add("@Area", OleDbType.VarChar)
odcommand.Parameters("@Area").Value = txtArea.Text
odcommand.Parameters.Add("@Address", OleDbType.VarChar)
odcommand.Parameters("@Address").Value = txtAddress.Text
odcommand.Parameters.Add("@Zipcode", OleDbType.VarChar)
odcommand.Parameters("@Zipcode").Value = txtZipcode.Text
odcommand.Parameters.Add("@Phone", OleDbType.VarChar)
odcommand.Parameters("@Phone").Value = txtPhone.Text
Dim updateCount As Integer
Try
updateCount = odcommand.ExecuteNonQuery()
MessageBox.Show("修改成功 " & updateCount & " 条")
Catch ex As Exception
MessageBox.Show("修改失败:" & ex.Message)
End Try
End Sub
两种方法都可以使用。
最后别忘了窗体关闭的时候,关闭数据库连接:
Private Sub Form6_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
odc.Close()
End Sub
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看vb.net 教程 目录
————————————————
版权声明:本文为CSDN博主「VB.Net」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/uruseibest/article/details/78882417
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比