0
我遇到的問題是我不斷捕獲SQL異常。它發生在我的'FlexList'類中,我運行了調試器,它是一行代碼「Dim mySqlDataReader As SqlDataReader = myCommand.ExecuteReader」,它正在破壞它並拋出錯誤,我做錯了什麼?提供的幫助將是非常有益的SqlDataReader在vb.net 2008中導致SqlException
查詢語句進入該類遇到錯誤:
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的 進口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
存儲過程我使用的測試: 進口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
...哪條線是錯誤?我看到一個可能的空引用'IIf(mySqlDataReader.IsDBNull(i),「NULL」,mySqlDataReader.GetValue(i).ToString)'。即使它是空的,它仍然會評估錯誤的部分。改變'IIF()'只是'If()'會解決這個問題。 – helrich
這個問題實際上是這樣回答的:「Dim mySqlDataReader As SqlDataReader = myCommand.ExecuteReader」 – cubitouch
我不想問這個問題,但是你不覺得這個問題中包含的實際異常是非常重要的嗎? –