簡體   English   中英

SqlDataReader在vb.net 2008中導致SqlException

[英]SqlDataReader is causing an SqlException in vb.net 2008

我遇到的問題是我不斷捕獲SQL異常。 它發生在我的“ FlexList”類中​​。我已經運行了調試器,正是這行代碼“ Dim mySqlDataReader As SqlDataReader = myCommand.ExecuteReader”打破了它並引發了錯誤。我在做什么錯?提供的幫助將非常有幫助。

進入類的查詢語句遇到錯誤:

Public Class MainView
    Private _thatList As New GetClass
    Private _AddEditTerms As New Add_EditWindow
    Private _listManager As New listManager
    Private myQuery As String = _thatList.getAvaliableStatuses
    Private _flexList As New FlexList(myQuery)
'cut out the rest of the code
End Class

遇到錯誤的類:Imports System.Data.SqlClient Imports System.Collections.Specialized

Public Class FlexList
    Public ReadOnly rows As New List(Of Dictionary(Of String, String))
    Public ReadOnly fields() As String

    Public Sub New(ByRef myQuery As String)
    Dim myConnection As SqlConnection = DataConnection.getProperityDBConnection()
    Dim row As Dictionary(Of String, String)

    Try
        myConnection.Open()
        Dim myCommand As New SqlCommand()
        myCommand.Connection = myConnection
        myCommand.CommandText = myQuery
        Dim mySqlDataReader As SqlDataReader = myCommand.ExecuteReader

        If mySqlDataReader.HasRows Then
            Dim fieldCount = mySqlDataReader.FieldCount
            ReDim fields(fieldCount - 1)

            mySqlDataReader.Read()
            For i = 0 To fieldCount - 1
                fields(i) = mySqlDataReader.GetName(i)
            Next

            Do
                row = New Dictionary(Of String, String)
                For i = 0 To fieldCount - 1
                    row.Add(mySqlDataReader.GetName(i), IIf(mySqlDataReader.IsDBNull(i), "NULL", mySqlDataReader.GetValue(i).ToString))
                Next
                rows.Add(row)
                row = Nothing
            Loop While mySqlDataReader.Read()

        End If

    Catch ex As SqlException
        MsgBox("Database Error. Please contact you system administor")
    Catch ex As DataException
        MsgBox("Connection Error. Please contact The Delevelopement Team")
    Catch ex As Exception
        MsgBox("An Unknown Error has occured. Try again and report the error if it persists: " & vbCr & ex.ToString)
    Finally
        myConnection.Close()
    End Try

End Sub

End Class

我用來測試此存儲過程:Imports System.Data.SqlClient

Public Class Get_Avaliable_Statuses
Public Function getAvailableStatuses()
    Dim connection As SqlConnection = DataConnection.getProperityDBConnection

    Dim insertCommand As New SqlCommand("dbo.ksp_Get_Available_Statuses", connection)
    insertCommand.CommandType = CommandType.StoredProcedure

    Try
        connection.Open()
        Dim count As Integer = insertCommand.ExecuteNonQuery()
        If count > 0 Then
            Return True
        Else
            Return False
        End If
    Catch ex As Exception
        Throw ex
    Finally
        connection.Close()
    End Try
End Function
End Class

有您的問題-“ [*]”使用“ *”。 刪除方括號

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM