gpt4 book ai didi

syntax-error - FROM子句vb.net中的语法错误

转载 作者:行者123 更新时间:2023-12-03 08:31:31 24 4
gpt4 key购买 nike

只是想让您知道这是我在此网站上发布的第一个问题,因此,如果我错过了任何内容,请告诉我。问题是我在程序运行时收到以下错误消息

FROM子句中的语法错误

代码在这里

    Dim dbProvider As String
Dim dbSource As String
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim MaxRows As Integer
Dim sql As String
Dim TableName As String
Dim TableCreate As New OleDb.OleDbCommand("CREATE TABLE [" & TableName & "](" & "ID INTEGER NOT NULL," & ")", con)
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM [" & TableName & "]", con)
Dim cb As New OleDb.OleDbCommandBuilder(da)

TableName = TbTableName.Text

dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = E:\A2 Computing\Project\PasswordDatabase.mdb"

con.ConnectionString = dbProvider & dbSource

con.Open()

da.Fill(ds, "NewTable")

MaxRows = ds.Tables("NewTable").Rows.Count

我的问题是在这一行
    da.Fill(ds, "NewTable")

我不确定我错过了什么,我在声明tablecreate或da时猜测与括号有关吗? = S任何帮助将不胜感激。 =)

最佳答案

您需要在使用TableName之前为其分配一些东西,而不是在之后。当前的SQL是SELECT * FROM [],这就是为什么在那里出现语法错误的原因。

更改代码顺序:

Dim TableName As String
TableName = TbTableName.Text

Dim TableCreate As New OleDb.OleDbCommand("CREATE TABLE [" & TableName & "](" & _
"ID INTEGER NOT NULL" & ")", con)
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM [" & TableName & "]", con)
Dim cb As New OleDb.OleDbCommandBuilder(da)

您还需要执行 TableCreate语句。 IIRC,您使用 ExecuteNonQuery这样做。
TableCreate.ExecuteNonQuery;

您的表创建SQL可以简化,顺便说一句:
' Remove unnecessary concatenation for column definition, as it's always
' the same
Dim TableCreate As New OleDb.OleDbCommand("CREATE TABLE [" & TableName & _
"](ID INTEGER NOT NULL)", con)

您实际上在哪里连接到数据库?在您的代码中的任何地方都看不到 OleDBConnection

这是MSDN文档中 OleDBCommand.ExecuteNonScalar的完整示例:
Private Sub CreateOleDbCommand( _
ByVal queryString As String, ByVal connectionString As String)
Using connection As New OleDbConnection(connectionString)
connection.Open()
Dim command As New OleDbCommand(queryString, connection)
command.ExecuteNonQuery()
End Using
End Sub

另外,您知道执行 CREATE TABLE会创建一个新的空表,因此您的 SELECT * FROM不会返回任何内容,只返回一行 NULL行,因为您没有插入任何数据吗?

关于syntax-error - FROM子句vb.net中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10490089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com