我在Visual Basic 2012中使用Windows窗體應用程序來使用.ADOX創建新的Microsoft Access數據庫。我可以創建數據庫,但不能在數據庫中創建表。在訪問數據庫中使用vb中的Adox創建表
我的代碼是:
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
我把問題推回去了。請不要更改原始問題 - 它會使針對該問題發佈的任何答案無效。 – LarsTech 2015-03-13 16:16:24