0

好的,我正在构建一个 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
4

2 回答 2

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
于 2012-10-25T11:15:24.523 回答
0

尝试使用 try-catch 语句:

在此处尝试
     您的代码以获取查询语句。
Catch ex as Exception
    MsgBox(ex.Message)
End Try

于 2012-10-25T10:42:59.157 回答