使用vb中的Adox在Access数据库中创建表

时间:2015-03-13 15:34:10

标签: vb.net vba ddl adox

我在Visual Basic 2012中使用Windows窗体应用程序使用.ADOX创建新的Microsoft Access数据库。我可以创建数据库但不能在数据库中创建表。

我的代码是:

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.

感谢任何帮助。

EDITED: 下面的代码超过了第一个错误,但我现在得到一个字段定义错误,我知道代码应该工作,因为它可以添加一个数字字段,可能因为访问中的字段类型是短文本和长文本但我和#39已经尝试过似乎没有用。

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

2 个答案:

答案 0 :(得分:0)

看起来你有太多的括号,但我认为实际的错误来自你在查询中的领先空间:

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

尝试将其更改为:

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

确保丢弃物品,最好是使用块:

示例:

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

答案 1 :(得分:0)

最后的代码是这样的:

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