更新datagridview和数据库vb.net

时间:2016-09-20 11:53:30

标签: database vb.net datagridview

我想更新我的datagridview以及我的数据库我是vb的新手,我不知道我做错了什么可以让sombody帮助我?

Private Sub DataGridView_Booking_Update(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView_Booking.RowLeave
    Try
        con.Open()
        Using cmd As New SqlCommand("UPDATE Tbl_booking SET Omschrijving = @Omschrijving, Jaar = @Jaar, Opmerking = @Opmerking ,Sortnr = @Sortnr)", con)
            cmd.Parameters.Add("@Omschrijving", SqlDbType.VarChar)
            cmd.Parameters.Add("@Jaar", SqlDbType.Int)
            cmd.Parameters.Add("@Opmerking", SqlDbType.VarChar)
            cmd.Parameters.Add("@Sortnr", SqlDbType.Int)
            cmd.ExecuteNonQuery()

        End Using
    Catch ex As Exception
        MessageBox.Show("Error while updating record on table..." & ex.Message, "Update Records")
    Finally
        con.Close()
    End Try

1 个答案:

答案 0 :(得分:-1)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        Dim con As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\database1.accdb;Persist Security Info=False;")
        con.Open()
        Dim cmd As New OleDb.OleDbCommand("INSERT INTO table1(name,age,class) VALUES('John Legend','22','B.A Part 1')", con)
        cmd.ExecuteNonQuery()
        MsgBox("Record inserted successfully.")
        con.Close()
    Catch
        MsgBox("Error Occured.")
    End Try
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Try
        Dim con As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\database1.accdb;Persist Security Info=False;")
        con.Open()
        Dim cmd As New OleDb.OleDbCommand("update table1 set name='John DOE',age='23',class='12th' where id='1'", con)
        cmd.ExecuteNonQuery()
        MsgBox("Record Updated Successfully.")
        con.Close()
    Catch
        MsgBox("Error Occured.")
    End Try
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Try
        Dim con As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\database1.accdb;Persist Security Info=False;")
        con.Open()
        Dim cmd As New OleDb.OleDbCommand("Delete from table1 where name='John DOE'", con)
        cmd.ExecuteNonQuery()
        MsgBox("Record Deleted Successfully.")
        con.Close()
    Catch
        MsgBox("Error Occured.")
    End Try
End Sub

Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged

    Me.Validate()
    Me.dbDataAdapter.Update(Me.dbdataset.Tables("MyTable"))

    Me.dbDataSet.AcceptChanges()

End Sub