第一次机会异常错误

第一次机会异常错误

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

问题描述

我要通过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

推荐答案

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.



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

08-22 20:13