[英]SqlDataReader is causing an SqlException in vb.net 2008
The problem I am having is I keep catching a SQL exception. 我遇到的问题是我不断捕获SQL异常。 It's happening in my 'FlexList" class. I've ran the debugger and it is the line of code "Dim mySqlDataReader As SqlDataReader = myCommand.ExecuteReader" that is breaking it and throwing the error. What am I doing wrong? Any kind of help offered would be EXTREMELY helpful.
它发生在我的“ FlexList”类中。我已经运行了调试器,正是这行代码“ Dim mySqlDataReader As SqlDataReader = myCommand.ExecuteReader”打破了它并引发了错误。我在做什么错?提供的帮助将非常有帮助。
Query statement going into the class encountering errors: 进入类的查询语句遇到错误:
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
Class encountering the errors: Imports System.Data.SqlClient Imports System.Collections.Specialized 遇到错误的类: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
The stored procedure I am using to test this: Imports System.Data.SqlClient 我用来测试此存储过程: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
There is your problem - " [*] " use " * ". 有您的问题-“ [*]”使用“ *”。 Remove square brackets
删除方括号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.