首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Adox.table自动增量属性设置为列?

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

Stack Overflow用户
提问于 2018-07-26 17:48:22
回答 1查看 647关注 0票数 1

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

误差

属性'Item‘是'ReadOnly’

代码语言:javascript
复制
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:更新的代码-仍然获得错误

此连接不能用于执行此操作。在此上下文中,它要么已关闭,要么无效。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-26 19:19:39

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

代码语言:javascript
复制
.Item("Key").Properties("AutoIncrement") = True

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

您有几个选项可以纠正这种情况。最明确的方法是显式指定要分配Value属性。

代码语言:javascript
复制
.Item("Key").Properties("AutoIncrement").Value = True

代码语言:javascript
复制
Dim prop As ADOX.Property = .Item("Key").Properties("AutoIncrement")
prop.Value = True

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

代码语言:javascript
复制
.Item("ContactId").Properties("AutoIncrement")() = True

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

有关更多信息,请参见:如何:在Visual中声明和调用默认属性

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51544783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档