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

时间:2014-02-04 09:47:11

标签: 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 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

但是这样做没有运气,因为它没有错误地崩溃。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

我自己解决了这个问题,如果你们中间有任何类似的问题在这里就是解决方案

 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() 

答案 1 :(得分:1)

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