vb.net 有此错误称为“System.Data.OleDb.OleDbException 类型的第一次机会异常发生在 System.Data.dll”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17678267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:16:47  来源:igfitidea点击:

having this error called "A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll"

vb.netms-access

提问by Ray Light

Good evening programmers. I'm doing my final year project and am doing a food ordering system based on VB.net with Access database. I have this error that keeps on appearing when i debug my application,

程序员们晚上好。我正在做我最后一年的项目,正在做一个基于 VB.net 和 Access 数据库的食品订购系统。我在调试应用程序时不断出现此错误,

A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll Additional information: Syntax error in INSERT INTO statement.

System.Data.dll 中出现类型为“System.Data.OleDb.OleDbException”的第一次机会异常附加信息:INSERT INTO 语句中的语法错误。

Here's a copy of the code i'm working on. This code is following a similar code which inputs admin's details. This particular code in this form intends to accept the Admin's ID and Password used to login into the system.

这是我正在处理的代码的副本。此代码遵循输入管理员详细信息的类似代码。此表单中的此特定代码旨在接受用于登录系统的管理员 ID 和密码。

Imports System.Data.OleDb
Imports System.Configuration

Public Class adminadd2

    'must put everytime, global bro
    Dim con As New OleDbConnection

    Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click

        Dim TA As New CQFOSDataSet1TableAdapters.adminloginTableAdapter
        Dim cmd As New OleDbCommand
        Dim con As New OleDbConnection


        'Dim yourconnection As String = ConfigurationManager.ConnectionStrings("yourconnectionstring").ConnectionString

        'con = New OleDbConnection(yourconnection)

        'cmd.CommandType = CommandType.Text

        Dim connectionString As String = ConfigurationManager.ConnectionStrings("Mark1.My.MySettings.CQFOSConnectionString").ConnectionString
        con = New OleDbConnection(connectionString)

        cmd.CommandType = CommandType.Text


        cmd.CommandText = "INSERT INTO adminlogin(Username,Password) VALUES(@Username,@Password)"


        cmd.Parameters.AddWithValue("@Username", adminid.Text)
        cmd.Parameters.AddWithValue("@Password", adminpass.Text)

        cmd.Connection = con

        Dim RowsAffected As Integer
        con.Open()

        'rowaffected returns the number or row affected
        RowsAffected = cmd.ExecuteNonQuery()

        Dim selectQuery As String = "Select FROM adminlogin"
        If RowsAffected = 1 Then
            MsgBox("New Admin is saved. Login Using your credentials.", MsgBoxStyle.OkOnly, "CQFOS")
        End If
        con.Close()

        Me.Hide()
        adminaccess.Show()

    End Sub
End Class

回答by Karl Anderson

There are first chance exceptions and second chance exceptions; second chance exceptions are the ones that your code attempt to handle, while first chance exceptions are "seen" by the debugger. In other words, first chance exceptions only matter when you are debugging.

有第一次机会例外和第二次机会例外;第二次机会异常是您的代码尝试处理的那些,而调试器“看到”第一次机会异常。换句话说,第一次机会异常仅在您进行调试时才重要。

Here is some MSDN documentation on First and second chance exception handling

这是有关第一次和第二次机会异常处理的一些 MSDN 文档

回答by HansUp

Passwordis a reserved word. Enclose it in square brackets in your INSERTstatement.

Password保留字。在您的INSERT陈述中将其括在方括号中。

cmd.CommandText = "INSERT INTO adminlogin(Username,[Password]) VALUES(@Username,@Password)"

回答by Mal

each time you run the application in the debugger it copies the access database from the project directory to the bin/debug directory thus overwriting the data you previously input whilst debugging.

每次在调试器中运行应用程序时,它都会将访问数据库从项目目录复制到 bin/debug 目录,从而覆盖您之前在调试时输入的数据。

Set the behaviour of the access dataset to Do Not Copy and your data will persist through debug sessions.

将访问数据集的行为设置为“不复制”,您的数据将通过调试会话保持不变。