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 教程 目录

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值