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

How to set Adox.table auto increment property to a column?

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

错误

Property 'Item' is 'ReadOnly'

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

P.S:更新代码 - 仍然出现错误

The connection cannot be used to perform this operation. It is either closed or invalid in this context.

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

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

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

此语句将True赋值给赋值语句左侧返回的ADOX.Property默认值属性。此语法对 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 中,默认属性通常称为索引器 属性 并采用整数参数。基于 COM 的默认值 属性 不需要参数,但要告诉 VB 编译器您要引用它,您需要额外的 () 且不包含任何参数。

有关详细信息,请参阅:How to: Declare and Call a Default Property in Visual Basic