SqlDataReader在vb.net 2008中导致SqlException

时间:2014-01-16 16:20:32

标签: .net vb.net sqldatareader sqlexception

我遇到的问题是我一直在捕捉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

类遇到错误:     导入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

1 个答案:

答案 0 :(得分:1)

有你的问题 - “[*]”使用“*”。删除方括号