【问题标题】:OleDBException with ExecuteScalar in vb.net在 vb.net 中带有 ExecuteScalar 的 OleDBException
【发布时间】:2016-01-14 19:56:13
【问题描述】:

我有一个实施了授权系统的应用程序。它有一个可以登录的用户访问 2013 数据库。该数据库包含三列:用户名、密码和位置。该职位将类似于员工或经理。

成功登录后,用户会看到主表单(form1.vb),但我发生了一些事情。首先更新一个标签,其中包含他们的用户名。而且我正在尝试实现一种方法来更新“位置”标签,具体取决于谁登录。

请记住,我是 vb.net 的新手。为此,我使用 ExecuteScalar 和以下代码:

            Using conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Joe\Desktop\Folders\Developing\inventoryManagement\inventoryManager.accdb")
        Using command As New OleDb.OleDbCommand("SELECT Position FROM im_users", conn)
            conn.Open()



            Dim position As String = CStr(command.ExecuteScalar())

            Label6.Text = position

        End Using
    End Using


但是,当我启动应用程序并登录时,它会出现以下异常:

错误代码=-2147467259 H结果=-2147467259 Message=IErrorInfo.GetDescription 失败,出现 E_FAIL(0x80004005)。 源=系统.数据 堆栈跟踪: 在 >System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult 小时) 在 >System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS >dbParams,对象和执行结果) 在 System.Data.OleDb.OleDbCommand.ExecuteCommandText(对象& >executeResult) 在 System.Data.OleDb.OleDbCommand.ExecuteCommand(命令行为>行为,对象和执行结果) 在 System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior > 行为,字符串方法) 在 System.Data.OleDb.OleDbCommand.ExecuteScalar() 在 C:\Users\Joe\Desktop\Folders\Developing\inventoryManagement\Home\Home\Form1.vb:>line 10 中的 Home.Form1.Form1_Load(Object sender, EventArgs e) 在 System.Windows.Forms.Form.OnLoad(EventArgs e) 在 System.Windows.Forms.Form.OnCreateControl() 在 System.Windows.Forms.Control.CreateControl(布尔 fIgnoreVisible) 在 System.Windows.Forms.Control.CreateControl() 在 System.Windows.Forms.Control.WmShowWindow(消息和 m) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ScrollableControl.WndProc(消息和 m) 在 System.Windows.Forms.Form.WmShowWindow(消息和 m) 在 System.Windows.Forms.Form.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& > m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,>Int32 msg,IntPtr wparam,IntPtr lparam) 内部异常:

您能告诉我如何纠正这个问题吗?

【问题讨论】:

  • 请编辑您的问题并在报价中发布异常详细信息
  • InnerException 文本?
  • 这是我按下“将例外复制到剪贴板”时的完整例外

标签: vb.net


【解决方案1】:

这个问题是由单词 POSITION 引起的,它是 Access Database Engine Reserved Word

通常的解决方法可以解决问题

Using command As New OleDb.OleDbCommand("SELECT [Position] FROM im_users", conn)

这里奇怪的是错误信息。通常,保留字的使用以Syntax Error 消息结尾,但在这种情况下(经过测试),确切的错误是上面发布的错误。

说我应该警告你。照原样,您的查询返回整个表,而不仅仅是属于特定用户的记录,因为您没有标识正确记录的 WHERE 子句。
此外,ExecuteScalar 返回查询提取的第一行第一列的值。您的代码始终从第一条记录中检索 POSITION,无论它是什么。

【讨论】:

  • 非常感谢!!我在某个地方看到过这个帖子,但我没有意识到“位置”是 Access 中的保留字。也谢谢你的链接。我会确保在未来检查:)
  • 啊,是的,我注意到我缺少 WHERE 子句。我现在添加了它,所以它返回登录(测试)用户的位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多