Vb.net 将更新从datagridview发送到数据库中的现有行

Vb.net 将更新从datagridview发送到数据库中的现有行,vb.net,datagridview,datatable,Vb.net,Datagridview,Datatable,我正在使用以下代码从数据库中检索记录 Dim SearchID As Integer = teacherID Dim NewStudentID As Integer = studentID Dim DisplayTable As New DataTable() Dim da As New OleDbDataAdapter() Dim sqlquery As String = ("select * from tblAppointments WHERE Tea

我正在使用以下代码从数据库中检索记录

    Dim SearchID As Integer = teacherID
    Dim NewStudentID As Integer = studentID
    Dim DisplayTable As New DataTable()
    Dim da As New OleDbDataAdapter()
    Dim sqlquery As String = ("select * from tblAppointments WHERE TeacherID =" & teacherID & "")
    If conn.State = ConnectionState.Closed Then
        conn.Open()
    End If
    Try
        da.SelectCommand = New OleDbCommand(sqlquery, conn)
        da.Fill(Finalds, "Display")
        DisplayTable = Finalds.Tables("Display")
        DisplayTable.Columns.Remove("Instrument")
        DisplayTable.Columns.Remove("Room")
        DisplayTable.Columns.Remove("TeacherID")
        Registersgridview.DataSource = DisplayTable
        Registersgridview.Columns(0).Visible = False
        conn.Close()
    Catch ex As Exception
        MsgBox("There are no appointments in the database for " + Tutorcombox.Text)
    End Try
它还添加到datagridview中,某些列被删除,一些列也被隐藏

因为它本质上是一个寄存器,所以当用户单击一个布尔值的datagridview字段时,它会从false变为true。我一直在尝试将此信息发送回数据库,但没有成功。我尝试了以下方法:

    Dim dt As DataTable = New DataTable("SendTable")
    Dim row As DataRow
    dt.Columns.Add("appID", Type.GetType("System.Int32"))
    dt.Columns.Add("Present", Type.GetType("System.Boolean"))
    For i = 0 To Registersgridview.Rows.Count - 1
        row = dt.Rows.Add
        row.Item("appID") = Registersgridview.Rows(i).Cells(0)
        row.Item("Present") = Registersgridview.Rows(i).Cells(5)
    Next
    If conn.State = ConnectionState.Closed Then
        conn.Open()
    End If
    Dim sqlquery As String = "Update tblAppointments SET Present = @Present WHERE appID = @appID"
    Dim sqlcommand As New OleDbCommand
    For Each newrow As DataRow In dt.Rows
        With sqlcommand
            .CommandText = sqlquery
            .Parameters.AddWithValue("@Present", newrow.Item(1))
            .Parameters.AddWithValue("@appID", newrow.Item(0))
            .ExecuteNonQuery()
        End With
        conn.Close()
    Next
但他们没有这样做的运气,因为它崩溃时没有错误


有人能帮忙吗?

我自己解决了这个问题,如果你们中有人有类似的问题,这里是解决办法

try this:

   Dim dt As DataTable = New DataTable("SendTable")
    Dim row As DataRow
    dt.Columns.Add("appID", Type.GetType("System.Int32"))
    dt.Columns.Add("Present", Type.GetType("System.Boolean"))
    For i = 0 To Registersgridview.Rows.Count - 1
        row = dt.Rows.Add
        row.Item("appID") = Registersgridview.Rows(i).Cells(0)
        row.Item("Present") = Registersgridview.Rows(i).Cells(5)
    Next
    If conn.State = ConnectionState.Closed Then
        conn.Open()
    End If
    Dim sqlquery As String = "Update tblAppointments SET Present = @Present WHERE appID = @appID"
    Dim sqlcommand As New OleDbCommand
    For Each newrow As DataRow In dt.Rows
        With sqlcommand
            .CommandText = sqlquery
            .Parameters.AddWithValue("@Present", newrow.Item(5))
            .Parameters.AddWithValue("@appID", newrow.Item(0))
            .ExecuteNonQuery()
        End With
        conn.Close()
    Next
 Dim dt As DataTable = New DataTable("SendTable")
        Dim row As DataRow
        dt.Columns.Add("appID", Type.GetType("System.Int32"))
        dt.Columns.Add("Present", Type.GetType("System.Boolean"))
        For i = 0 To Registersgridview.Rows.Count - 1
            Dim appID As Integer = Registersgridview.Rows(i).Cells(0).Value
            Dim present As Boolean = Registersgridview.Rows(i).Cells(4).Value
            row = dt.Rows.Add
            row.Item("appID") = appID
            row.Item("Present") = present
        Next
        If conn.State = ConnectionState.Closed Then
            conn.Open()
        End If
        Dim sqlquery As String = "UPDATE tblAppointments SET Present = @Present WHERE appID = @appID"
        Dim sqlcommand As New OleDbCommand
        For Each newrow As DataRow In dt.Rows
            With sqlcommand
                .CommandText = sqlquery
                .Parameters.AddWithValue("@Present", newrow.Item(1))
                .Parameters.AddWithValue("@appID", newrow.Item(0))
                .Connection = conn
                .ExecuteNonQuery()
            End With
        Next
        conn.Close()
        Registersgridview.DataSource = Nothing
        dt.Clear()