在Access数据库中编辑未绑定的记录

时间:2013-11-30 14:54:39

标签: database vb.net

我终于让它工作似乎我忘记了一些刻度线。下面的代码似乎有效。

如果有人发现任何严重错误,我希望他们能让我知道。

    connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & PathCMDB() & "';"

    sql = "Update [Machine Attributes] set [VALUE] =  " & e.Value & " Where " & _
            "([Machine Attributes].[Machine ID] =" & m_machineID & _
            " ) And ([Machine Attributes].[Attribute Type ID] = " & e.Row.Tag & " ) ;"

    connection = New OleDbConnection(connetionString)

    Try
        connection.Open()

        Dim cmd As OleDbCommand = New OleDbCommand(sql, connection)

        Dim check As Integer = cmd.ExecuteReader.RecordsAffected
        cmd.Connection.Close()
        connection.Open()

        If check < 1 Then 'We need to add the record that does not exist

            sql = "INSERT INTO [Machine Attributes]([Attribute Type ID],[Machine ID],[Value]) VALUES('" & _
                    e.Row.Tag & "','" & m_machineID & "','" & e.Value & "')"

            cmd = New OleDbCommand(sql, connection)


        End If

        cmd.ExecuteNonQuery()

        connection.Close()

    Catch ex As Exception

        MessageBox.Show("Can not open connection ! ")

    End Try

1 个答案:

答案 0 :(得分:1)

易。只需启动执行更新命令的非查询

这是一些伪代码。只需修改SQL字符串变量。

    Dim connection As OleDbConnection
    dim sql as string = "Update [Machine Attributes] set [field to edit] =  " & controlwhithdata.property & " Where " & _
        "[Key field] = " & controlwithkeyfield.property & ";"
    Dim oledbcmD As OleDbCommand(sql, connection)
    Dim connetionString As String



    connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & PathCMDB() & "';"
    Try

    connection = New OleDbConnection(connetionString)
    connection.Open()


        oledbcmD.ExecuteNonQuery()

        connection.Close()

    Catch ex As Exception
         connection.close()
         msgbox(ex.tostring)
    End Try

旁注您可能希望使用“读取器”从数据库中获取要控制的数据,而不是使用适配器。