VB 2012:使用ADO记录集添加新的修改记录MSACCESS

时间:2016-02-01 03:32:16

标签: vb.net

我尝试了以下代码, 1)Button3_Click()...为了添加新记录,没有错误消息,但没有记录添加到数据库 2)Button2_Click()...对于更新记录,显示错误消息“无法从程序集'WindowsApplication1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'加载类型'ADODB.FieldsToInternalFieldsMarshaler'。”

有人可以为此提供帮助。

Public Class Form1



Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'update 1st record
    Dim rs As New ADODB.Recordset

    Dim cn As New ADODB.Connection()
    cn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=Test.mdb;"
    cn.Open()

    rs.Open("SELECT * FROM Users", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockBatchOptimistic)


    rs.MoveFirst()
    rs.Fields("EmpName").Value = "AAAAA"
    rs.Fields("EmpId").Value = "222"
    rs.UpdateBatch()

    rs.Close()
    cn.Close()
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    'add new Record

    Dim rs As New ADODB.Recordset

    Dim cn As New ADODB.Connection()
    cn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=Test.mdb;"
    cn.Open()

    rs.Open("SELECT * FROM Users", cn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockBatchOptimistic)

    Dim fieldsArray(1) As Object
    fieldsArray(0) = "EmpName"
    fieldsArray(1) = "EmpId"
    Dim values(1) As Object
    values(0) = "4"
    values(1) = "as"

    rs.AddNew(fieldsArray, values)
    rs.UpdateBatch()

    rs.Close()
    cn.Close()
End Sub

结束班

1 个答案:

答案 0 :(得分:0)

查看我测试的代码并且它有效:Table1有两个字段。我添加了一个包含两个值的行。

    Dim cnn As New ADODB.Connection
    Set cnn = CurrentProject.Connection
    Dim rs As New ADODB.Recordset
    Set rs = New ADODB.Recordset
    rs.Open "Table1", cnn, adOpenKeyset, adLockOptimistic

    rs.AddNew
    rs.Fields(1) = "a"
    rs.Fields(2) = "b"
    rs.UpdateBatch