第一次机会异常错误 [英] First chance exception error

查看:91
本文介绍了第一次机会异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要通过vb.net向ms access 2010添加一些数据,我在运行时收到以下错误消息。



第一次机会异常在System.Data.dll中输入'System.Data.OleDb.OleDbException'



我尝试过:



我的代码是;



I am going add some data to ms access 2010 by vb.net and I am getting following error message at run time.

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

What I have tried:

My Code is;

Imports System.Data.OleDb
Public Class Form1

Private Sub cmdcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdcancel.Click
        Close()
    End Sub

    Private Sub cmdadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdadd.Click
        Try
            Dim sqlconn As New OleDb.OleDbConnection
            Dim sqlquery As New OleDb.OleDbCommand
            Dim connString As String
            connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Tubewell\Tubewell\Tubewell.accdb"
            sqlconn.ConnectionString = connString
            sqlquery.Connection = sqlconn
            sqlconn.Open()
            sqlquery.CommandText = "INSERT INTO Employee_Details(EmpNo,Employee Name,Status) VALUES ('" & txtempno.Text & "','" & txtempname.Text & "','" & ComboBox1.Text & "')"
            sqlquery.ExecuteNonQuery()
            sqlconn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class

推荐答案

首先修复 SQL注入 [ ^ ]漏洞。



你还需要包装方括号中的员工姓名字段。您通常应该避免在名称中创建带空格或其他特殊字符的列或表。



您应该在中包装连接和命令对象阻止,以确保它们始终被正确清理。

Start by fixing the SQL Injection[^] vulnerability.

You'll also need to wrap the Employee Name field in square brackets. You should generally avoid creating columns or tables with spaces or other special characters in their name.

You should wrap the connection and command objects in Using blocks, to ensure that they're always cleaned up properly.
Private Sub cmdadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdadd.Click
    Try
        Using sqlconn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Tubewell\Tubewell\Tubewell.accdb")
            Using sqlquery As New OleDb.OleDbCommand("INSERT INTO Employee_Details(EmpNo, [Employee Name], Status) VALUES (?, ?, ?)", sqlconn)
                sqlquery.Parameters.AddWithValue("@EmpNo", txtempno.Text)
                sqlquery.Parameters.AddWithValue("@Name", txtempname.Text)
                sqlquery.Parameters.AddWithValue("@Status", ComboBox1.Text)
                 
                sqlconn.Open()
                sqlquery.ExecuteNonQuery()
            End Using
        End Using
    
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub



如果您仍然收到异常,请在 MessageBox.Show 行上设置断点,并检查异常的完整细节。


If you're still getting the exception, set a breakpoint on the MessageBox.Show line, and examine the full details of the exception.


删除Try / Catch,它将允许您确切地查看问题的位置,并且使用调试器,您将能够检查变量。

-----

不是你问题的解决方案,而是你遇到的另一个问题。

永远不要通过连接来构建SQL查询字符串。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

SQL注射预防备忘单 - OWASP [ ^ ]
Remove the Try/Catch, it will allow you to see exactly where is the problem and with the debugger, you will be able to inspect variables.
-----
Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


这篇关于第一次机会异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆