vb.net 教程 8-3 数据库操作10-2
发布时间:2020-12-16 23:53:12 所属栏目:大数据 来源:网络整理
导读:接上一节,下面开始正式进行修改: 使用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.Comman
接上一节,下面开始正式进行修改: 使用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 教程 目录 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |