gpt4 book ai didi

database - 无法用数据填充 OleDbDataAdapter

转载 作者:搜寻专家 更新时间:2023-10-30 21:48:10 25 4
gpt4 key购买 nike

我无法在 OleDbDataAdapter 中填充数据

我收到一个包含以下消息的 OleDbExceptionFROM 子句中的语法错误。

在这一行 adapter.Fill(dataset, "User")

这是代码

Imports System.Data.OleDb

Public Class Form1

Public connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\MyDatabase.mdb;Persist Security Info=False")

Private Sub button_display_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_display.Click
connection.Close()
connection.Open()
Dim adapter As New OleDbDataAdapter
adapter.SelectCommand = New OleDbCommand
adapter.SelectCommand.Connection = connection
adapter.SelectCommand.CommandText = "select * from user;"

Dim dataset As New DataSet
adapter.Fill(dataset, "User")
connection.Close()
Dim datarow = dataset.Tables("User").Rows(0)
Dim fname, lname As String
fname = datarow(0)
lname = datarow(1)
txtfirstname.Text = fname
txtlastname.Text = lname

End Sub
End Class

我试过替换

    Dim adapter As New OleDbDataAdapter
adapter.SelectCommand = New OleDbCommand
adapter.SelectCommand.Connection = connection
adapter.SelectCommand.CommandText = "select * from user;"

Dim dataset As New DataSet
adapter.Fill(dataset, "User")

    Dim adapter As New OleDbDataAdapter("select * from user;", connection)
Dim dataset As New DataSet
adapter.Fill(dataset, "User")

我也试过了

    Dim adapter As New OleDbDataAdapter
adapter.SelectCommand = New OleDbCommand("select * from user;", connection)
Dim dataset As New DataSet
adapter.Fill(dataset)

但我得到了同样的异常FROM 子句中的语法错误

最佳答案

User 是一个保留字。应该这样写:

"select * from [user];"

注意方括号。最好通过重命名表来完全避免保留字。

关于database - 无法用数据填充 OleDbDataAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5282776/

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