在 vb 中使用 Adox 在访问数据库中创建 table

create table in access database using Adox in vb

我在 Visual Basic 2012 中使用 Windows 表单应用程序来使用 .ADOX 创建新的 Microsoft Access 数据库。我可以创建数据库,但无法在数据库中创建 table。

我的代码是:

Imports ADOX
Imports System.Data.OleDb
Public Class Form1
Dim mycommand As OleDbCommand
Dim myconnection As OleDbConnection
Dim myReader As OleDbDataReader
Dim str As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim cat As New Catalog()
    Dim tablename As String = "Users"
    cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\important\DDL.mdb;Jet OLEDB:Engine Type=5")
    cat = Nothing
    myconnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\important\DDL.mdb ")
    myconnection.Open()
    str = "CREATE TABLE [ " & tablename & "] ([Username] varchar(50)), ([Password] varchar(50)), ([E-mail] varchar(75))"
    mycommand = New OleDb.OleDbCommand(str, myconnection)
    mycommand.ExecuteNonQuery()
    MsgBox("Database created")
End Sub
End Class

得到的错误是:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Syntax error in CREATE TABLE statement.

感谢任何帮助。

已编辑: 下面的代码通过了第一个错误,但我现在得到了一个字段定义错误,我知道代码应该可以工作,因为它可以添加一个数字字段,这可能是因为访问中的字段类型是短文本和长文本,但我已经试了好像不行。

Imports ADOX
Imports System.Data.OleDb
Public Class Form1
Dim mycommand As OleDbCommand
Dim myconnection As OleDbConnection
Dim myReader As OleDbDataReader
Dim str As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim cat As New Catalog()
    cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\important\DDL.mdb;Jet OLEDB:Engine Type=5")
    cat = Nothing
    myconnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\important\DDL.mdb ")
    myconnection.Open()
    str = "CREATE TABLE [Users] (Username Varchar(50), Password Varchar(50), E-mail Varchar(50))"

    mycommand = New OleDb.OleDbCommand(str, myconnection)
    mycommand.ExecuteNonQuery()
    MsgBox("Database created")
End Sub
End Class

看起来你的括号太多了,但我认为实际错误来自你查询中的前导 space:

                     V
str = "CREATE TABLE [ " & tablename & "]

尝试将其更改为:

str = "CREATE TABLE [" & tablename & "] ([Username] varchar(50), [Password] varchar(50), [E-mail] varchar(75))"

确保处理对象,最好是在 using 块中:

示例:

Using cn As New OleDb.OleDbConnection(mdb)
  cn.Open()
  Using cmd As New OleDb.OleDbCommand(str, cn)
    cmd.ExecuteNonQuery()
  End Using
End Using

最后的代码如下所示:

Imports ADOX
Imports System.Data.OleDb
Public Class Form1
    Dim mycommand As OleDbCommand
    Dim myconnection As OleDbConnection
    Dim myReader As OleDbDataReader
    Dim str As String
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim cat As New Catalog()
        cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Test\DDL.mdb;Jet OLEDB:Engine Type=5")
        cat = Nothing
        Using myconnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Test\DDL.mdb ")
            myconnection.Open()
            str = "CREATE TABLE [Users] ([Username] varchar(50), [Password] varchar(50), [E-mail] varchar(50))"

            Using mycommand As New OleDb.OleDbCommand(str, myconnection)
                mycommand.ExecuteNonQuery()
            End Using
        End Using
        MsgBox("Database created")
    End Sub
End Class