【发布时间】:2014-06-23 12:55:45
【问题描述】:
我一定完全遗漏了一些东西,因为我的 MS SQL 2008 数据表中有各种数据类型字段,并且能够正确检索除我新添加的 GUID 数据(唯一标识符类型)之外的所有数据。 我的 .net 应用程序为 GUID 字段设置了一个阅读器,但是当我单步执行代码并分配给本地对象时,其中从来没有任何数据,它只是一个空的 GUID。 在 SSMS 中运行相同的存储过程可以清楚地显示结果集中提取的 GUID。
谁能给我任何关于可能出错的线索或一些调试技巧? (Visual Studio 2008 .net 3.5、vb.net、MS SQL Server 2008 r1)
这是我的 vb.net iDataReader
Public Class DataReaderHelper
Implements IDataReader
Private _Reader As IDataReader
Private strFields() As String
Public Sub New(ByVal _DataReader As IDataReader)
' Load the fields array with uppercased field names in the reader
ReDim strFields(0 To _DataReader.FieldCount - 1)
For iIndex As Integer = 0 To _DataReader.FieldCount - 1
strFields(iIndex) = _DataReader.GetName(iIndex).ToUpper
Next
_Reader = _DataReader
End Sub
Public Function FieldIndex(ByVal FieldName As String) As Integer
' Returns the ordinal position of the named field
' in the datareader, or a -1 if the field is not found
Return Array.IndexOf(strFields, FieldName.ToUpper)
End Function
#Region "SetObjectProperty overloads"
' Type-specific overloads to allow data object properties to
' be consistently populated with values if the encapsulated
' data reader contains data for the specified field name.
Public Sub SetObjectProperty(ByVal FieldName As String, _
ByRef ObjectProperty As Guid)
' Set the GUID object property if the referenced field exists
' in the data reader. If the field does not exist or is null,
' set the property to an empty GUID.
Dim GuidValue As Guid
Dim intIndex As Integer = FieldIndex(FieldName)
If intIndex > -1 AndAlso Not _Reader.IsDBNull(intIndex) Then
GuidValue = _Reader.GetGuid(intIndex)
End If
ObjectProperty = GuidValue
End Sub
字段名正确,列索引正确。
我单步执行了这段代码,但这条线似乎对获取价值没有任何作用。
GuidValue 总是 EMPTY 就好像 _Reader.GetGuid(intIndex) 什么都不做一样。
任何帮助将不胜感激,如果您需要更多信息,请告诉我。
【问题讨论】:
-
你有什么数据?您确定这是您在正确位置的指南吗?你确定它进入你的 IF 你应该从你的问题中删除不必要的代码,只显示相关部分。
-
哈,显然你要挂断不必要的代码来阅读初始文本。 “我执行代码并进行分配”。我还解释了我正在使用的数据。如果我 100% 确定什么是不必要的,我可能会解决这个问题,最好有太多而不是不够理解。据我所知,唯一必要的代码行是: GuidValue = _Reader.GetGuid(IntIndex).
-
好吧,如果你得到空回,那是因为它是被选中的值。因此,“你有什么数据”的问题。
-
看不到如何返回除 GUID 之外的任何内容。我刚刚放弃并修改了 SP 以在拾取时将其更改为字符串,更改了我的数据对象以将其视为字符串并且 ta da!它在应用程序中。不是 EMPTY,而是 GUID 的字符串表示形式。仍然不知道为什么读者无法将其作为 GUID 读取,但我还是打算稍后将其转换为字符串。所以我已经完成了,不能再浪费时间了。
标签: sql-server vb.net sql-server-2008