1

.Item("Key").Properties("AutoIncrement") = True未将列类型设置为自动增量编号。它说它是只读的,但它在微软官方网站上。这是在较旧的未更新版本的 microsoft docs https://docs.microsoft.com/en-us/previous-versions/office/developer/office2000/aa164917(v=office.10) 似乎它不起作用现在对于 Visual Studio 2012 vb.net 如何将列“key”设置为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
4

1 回答 1

3

用作 VB.Net 代码基础的原始代码是用 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 编译器您要引用它,您需要额外的(),而不需要任何封闭的参数。

有关详细信息,请参阅:如何:在 Visual Basic 中声明和调用默认属性

于 2018-07-26T19:19:39.660 回答