如何在VB.NET 2010代码中捕获SQL异常?

时间:2012-10-25 10:39:12

标签: sql vb.net ms-access

好的,我正在构建一个VB.NET系统但是我在代码的某些部分捕获SQL异常时遇到了麻烦。简而言之,我使用SELECT sql查询来检索特定记录,我想确定MSAccess数据库中是否存在记录,以便我不检索0行。在打印到TextField期间,0行将导致异常。以下是我正在处理的示例代码:

    If txSearch.Text = "" Then
        MsgBox("Please type the user id or use the barcode reader to scan",     MsgBoxStyle.OkOnly, "Search Field Empty")
    End If

    'ElseIf txSearch.Text != "" Then
    If txSearch.Text <> "" Then
        Dim con As New OleDb.OleDbConnection
        Dim dbProvider As String
        Dim dbSource As String
        Dim ds As New DataSet
        Dim dt As New DataSet
        Dim da As OleDb.OleDbDataAdapter
        Dim de As OleDb.OleDbDataAdapter
        Dim sql As String
        Dim sql1 As String
        Dim temp_num As Integer
        Dim cmd As New OleDb.OleDbCommand

        dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        dbSource = "Data Source = new.mdb"

        con.ConnectionString = dbProvider & dbSource

        con.Open()

        Dim search As String
        search = txSearch.Text
        MessageBox.Show("You are going to search for " + search + ". Click OK to continue.")

        sql = "SELECT * FROM Student_Details WHERE Admin_No = '" & txSearch.Text & "'"

        sql1 = "SELECT * FROM Laptop_Details WHERE Admin_No = '" & txSearch.Text & "'"

        da = New OleDb.OleDbDataAdapter(sql, con)

        de = New OleDb.OleDbDataAdapter(sql1, con)

        'Dim check As Integer = sql.
        'If check > 0 Then

        'da.SelectCommand = cmd
        ds = New DataSet("Student_Details")
        da.Fill(ds, "Student_Details")
        dt = New DataSet("Laptop_Details")
        de.Fill(dt, "Laptop_Details")
        'con.Close()

        'If sql <> "" And sql1 <> "" Then
        'If ds.Equals(1) And ds.Equals(1) Then
        txAdminNO.Text = ds.Tables("Student_Details").Rows(0).Item(0)
        txName.Text = ds.Tables("Student_Details").Rows(0).Item(1)
        txProgramme.Text = ds.Tables("Student_Details").Rows(0).Item(2)
        cmbSStatus.SelectedText = ds.Tables("Student_Details").Rows(0).Item(3)
        txSerial.Text = dt.Tables("Laptop_Details").Rows(0).Item(1)
        txModel.Text = dt.Tables("Laptop_Details").Rows(0).Item(2)

        Dim com As New OleDb.OleDbCommand(sql, con)
        Dim com1 As New OleDb.OleDbCommand(sql1, con)

        Try

            temp_num = com.ExecuteNonQuery
            temp_num = com1.ExecuteNonQuery

        Catch ex As IndexOutOfRangeException
            Trace.WriteLine(ex.ToString)

        End Try

        con.Close()
        'End If
        'End If
    End If

2 个答案:

答案 0 :(得分:0)

尝试使用try-catch语句:

尝试
您的代码用于查询语句。
C ex ex Exception
MsgBox(ex.Message)
结束尝试

答案 1 :(得分:0)

尝试这样的事情:

 if ds.Tables("Student_Details").Rows.Count > 0  
    txAdminNO.Text = ds.Tables("Student_Details").Rows(0).Item(0)
    txName.Text = ds.Tables("Student_Details").Rows(0).Item(1)
    txProgramme.Text = ds.Tables("Student_Details").Rows(0).Item(2)
    cmbSStatus.SelectedText = ds.Tables("Student_Details").Rows(0).Item(3)

 End if

 if ds.Tables("Laptop_Details").Rows.Count > 0
    txSerial.Text = dt.Tables("Laptop_Details").Rows(0).Item(1)
    txModel.Text = dt.Tables("Laptop_Details").Rows(0).Item(2)
 End if

为了帮助您发表评论,我会这样做:

 if ds.Tables("Student_Details").Rows.Count > 0 And ds.Tables("Laptop_Details").Rows.Count > 0
    txAdminNO.Text = ds.Tables("Student_Details").Rows(0).Item(0)
    txName.Text = ds.Tables("Student_Details").Rows(0).Item(1)
    txProgramme.Text = ds.Tables("Student_Details").Rows(0).Item(2)
    cmbSStatus.SelectedText = ds.Tables("Student_Details").Rows(0).Item(3)
    txSerial.Text = dt.Tables("Laptop_Details").Rows(0).Item(1)
    txModel.Text = dt.Tables("Laptop_Details").Rows(0).Item(2)
 else
  MsgBox("The user with id "+txSearch.Text+" does not exist") 
 end if