无法用数据填充 OleDbDataAdapter

发布于 2024-10-21 10:43:51 字数 1805 浏览 0 评论 0原文

我无法在 OleDbDataAdapter 中填充数据

我收到带有以下消息的 OleDbException FROM 子句中的语法错误。

在这一行 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 子句中的语法错误

I am unable to fill data in the OleDbDataAdapter

I get an OleDbException with the following message
Syntax error in FROM clause.

at this line adapter.Fill(dataset, "User")

Here is the code

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

I have tried replacing

    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")

with

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

and also I have tried

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

But I get the same exception Syntax error in FROM clause

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒数 2024-10-28 10:43:51

用户是保留字。它应该这样写:

"select * from [user];"

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

User is a reserved word. It should be written like so:

"select * from [user];"

Note the square brackets. It would be better to avoid reserved words altogether by renaming the table.

对岸观火 2024-10-28 10:43:51

我不确定adapter.SelectCommand = New OleDbCommand是否正确(我不做太多VB工作,所以我可能是错的)无论如何,为什么你要关闭连接,然后打开它,然后不关闭它?将你的接近结束。

尝试这样的事情:

Dim conn As New OleDbConnection(connection)
Dim adapter As New OleDbDataAdapter()
conn.Open()
adapter.SelectCommand = new OleDbCommand("Select * From User", conn)
DataSet dataset As New DataSet
adapter.Fill(dataset)
conn.Close()

'其余的代码

I'm not certain adapter.SelectCommand = New OleDbCommand is correct (I don't do much VB so I could be wrong) In any event why are you closing the connection and then opening it and then not closing it? Move your close to the end.

Try something like this:

Dim conn As New OleDbConnection(connection)
Dim adapter As New OleDbDataAdapter()
conn.Open()
adapter.SelectCommand = new OleDbCommand("Select * From User", conn)
DataSet dataset As New DataSet
adapter.Fill(dataset)
conn.Close()

'Rest of your code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文