如何将Adox.table自动递增属性设置为列?

时间:2018-07-26 17:48:22

标签: sql vb.net adox

.Item("Key").Properties("AutoIncrement") = True没有将列类型设置为自动递增编号。它说它是只读的,但是在Microsoft官方网站上。这是在较旧的Microsoft文档https://docs.microsoft.com/en-us/previous-versions/office/developer/office2000/aa164917(v=office.10)的非更新版本中 似乎对于Visual Studio 2012 vb.net现在不起作用 如何将“键”列设置为auto increment number

错误

  

属性“项目”为“只读”

Imports ADOX 
Imports ADOX.DataTypeEnum

   Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click

            Dim DB1_file_name As String = "\DB3.mdb"
            Dim catDB As ADOX.Catalog
            Dim tblNew As ADOX.Table
            Dim catstring As String

            catDB = New ADOX.Catalog
            ' Open the catalog.
            'catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.UserAppDataPath & "\DB1.mdb"
            catstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.UserAppDataPath & DB1_file_name
            catDB.Create(catstring)
            'catDB.ActiveConnection = catstring

            tblNew = New ADOX.Table
            ' Create a new Table object.
            With tblNew
                .Name = "Contacts"


                With .Columns
                    .Append("Key", adInteger)
                    .Item("Key").Properties("AutoIncrement") = True
                    .Append("FirstName", adVarWChar)
                    .Append("LastName", adVarWChar)
                    .Append("Phone", adVarWChar)
                    .Append("Notes", adLongVarWChar)

                End With
            End With

            ' Add the new Table to the Tables collection of the database.
            catDB.Tables.Append(tblNew)

            catDB = Nothing
        End Sub

PS:更新的代码-仍然出现错误

  

该连接不能用于执行此操作。在这种情况下,它是关闭的或无效的。

Imports ADOX 
Imports ADOX.DataTypeEnum

   Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click

            Dim DB1_file_name As String = "\DB3.mdb"
            Dim catDB As ADOX.Catalog
            Dim tblNew As ADOX.Table
            Dim catstring As String

            catDB = New ADOX.Catalog
            ' Open the catalog.
            'catDB.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.UserAppDataPath & "\DB1.mdb"
            catstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.UserAppDataPath & DB1_file_name
            catDB.Create(catstring)
            'catDB.ActiveConnection = catstring

            tblNew = New ADOX.Table
            ' Create a new Table object.
            With tblNew
                .Name = "Contacts"
                .ParentCatalog = catDB

                With .Columns
                    .Append("Key", adInteger)
                    .Item("Key").Properties("AutoIncrement").Value = True
                    .Append("FirstName", adVarWChar)
                    .Append("LastName", adVarWChar)
                    .Append("Phone", adVarWChar)
                    .Append("Notes", adLongVarWChar)

                End With
            End With

            ' Add the new Table to the Tables collection of the database.
            catDB.Tables.Append(tblNew)

            catDB = Nothing
        End Sub

1 个答案:

答案 0 :(得分:1)

您用作VB.Net代码基础的original code是用VBA编写的。

.Item("Key").Properties("AutoIncrement") = True

此语句将True分配给赋值语句左侧返回的ADOX.Property default 属性。此语法对VBA有效,但对VB.Net无效。 ADOX.Property对象的默认属性是其Value属性。

您可以选择一些方法来更正此问题。最明确的方法是明确指定您要分配Value属性。

.Item("Key").Properties("AutoIncrement").Value = True

Dim prop As ADOX.Property = .Item("Key").Properties("AutoIncrement")
prop.Value = True

您还可以使用此语法引用默认属性。

.Item("ContactId").Properties("AutoIncrement")() = True

在.Net中,默认属性通常称为indexer属性,并带有一个整数参数。基于COM的默认属性不需要参数,但是要告诉VB编译器您要引用它,则需要额外的()而没有任何封闭的参数。

有关更多信息,请参见:How to: Declare and Call a Default Property in Visual Basic