在vb.net

时间:2016-01-14 19:56:13

标签: vb.net

我有一个实施授权系统的应用程序。它具有访问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


但是,当我启动应用程序并登录时,会出现以下异常:
< System.Data.OleDb.OleDbException未处理

  

错误码= -2147467259    的HResult = -2147467259    Message = IErrorInfo.GetDescription失败,带有E_FAIL(0x80004005)。    来源= System.Data    堆栈跟踪:         at> System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)         at> System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS> dbParams,Object& executeResult)         在System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object&> executeResult)         在System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior> behavior,Object& executeResult)         在System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior> behavior,String method)         在System.Data.OleDb.OleDbCommand.ExecuteScalar()         at Home.Form1.Form1_Load(Object sender,EventArgs e)位于C:\ Users \ Joe \ Desktop \ Folders \ Developing \ inventoryManagement \ Home \ Home \ Form1.vb:>第10行         在System.Windows.Forms.Form.OnLoad(EventArgs e)         在System.Windows.Forms.Form.OnCreateControl()         在System.Windows.Forms.Control.CreateControl(Boolean 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(Message&> m)         在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)        在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,> Int32 msg,IntPtr wparam,IntPtr lparam)    InnerException:

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

1 个答案:

答案 0 :(得分:0)

这个问题是由Access Database Engine Reserved Word

这个单词POSITION引起的

通常的workaroud修复了问题

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

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

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

相关问题