无法用数据填充 OleDbDataAdapter
我无法在 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 messageSyntax 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用户是保留字。它应该这样写:
注意方括号。最好通过重命名表来完全避免保留字。
User is a reserved word. It should be written like so:
Note the square brackets. It would be better to avoid reserved words altogether by renaming the table.
我不确定adapter.SelectCommand = New OleDbCommand是否正确(我不做太多VB工作,所以我可能是错的)无论如何,为什么你要关闭连接,然后打开它,然后不关闭它?将你的接近结束。
尝试这样的事情:
'其余的代码
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:
'Rest of your code