dataReader.GetValue(i)抛出System.InvalidOperationException

时间:2015-05-06 16:03:13

标签: c# .net oracle11g datareader

.Net 4.5 / WPF / MVVM / ADO.Net / Oracle 11g / Oracle.DataAccess.Client

使用IDataReader界面

所以,我有sealed BusinessObject构建器类,它接收一个DataReader对象,根据PropertyMap转换该DataReader中每个字段内的数据 - 从{1}}检索class - 并返回PropertyMap

然后将此ListItem添加到List中,然后使用List处理数据,在View上显示数据等。

整个过程在我的应用程序内部的其他地方工作得很好,今天我尝试使用BusinessObject构建器进程将一组新记录转换为List并在我的DashBoard中使用它。

此entiry的属性图如下所示。

ListItem

但是,只要该过程遇到下面的- pinfo Count = 9 System.Collections.Generic.Dictionary<string,System.Reflection.PropertyInfo> + [0] {[LATEST_LOAD_TIME, System.DateTime LATEST_LOAD_TIME]} + [1] {[LAST_DELIVERY, System.DateTime LAST_DELIVERY]} + [2] {[MAXVALDATE, System.DateTime MAXVALDATE]} + [3] {[MINVALDATE, System.DateTime MINVALDATE]} + [4] {[COMPLETED, Int32 COMPLETED]} + [5] {[FAILED, Int32 FAILED]} + [6] {[IN_PROGRESS, Int32 IN_PROGRESS]} + [7] {[NOT_STARTED, Int32 NOT_STARTED]} + [8] {[TOTAL, Int32 TOTAL]} 子句

if ()

它抛出 System.InvalidOperationException 异常。请参阅下面的完整例外详情。

for (int i = 0; i < dataReader.FieldCount; i++)
{
    if (dataReader.GetValue(i) != DBNull.Value)
    {
        try
        {   

我知道异常说

  

消息&#34;由于当前状态,操作无效   。对象&#34;

我认为DataReader可能已关闭,但事实并非如此。见下面的状态

+       dataReader  {Oracle.DataAccess.Client.OracleDataReader} System.Data.IDataReader {Oracle.DataAccess.Client.OracleDataReader}
-       dataReader.GetValue(i)  'dataReader.GetValue(i)' threw an exception of type 'System.InvalidOperationException'  object {System.InvalidOperationException}
    -           base    {"Operation is not valid due to the current state of the object."}  System.SystemException {System.InvalidOperationException}
        -       base    {"Operation is not valid due to the current state of the object."}  System.Exception {System.InvalidOperationException}
            +       Data    {System.Collections.ListDictionaryInternal} System.Collections.IDictionary {System.Collections.ListDictionaryInternal}
                        HelpLink    null    string
                        HResult -2146233079 int
            +       InnerException  null    System.Exception
                        Message "Operation is not valid due to the current state of the object."    string
                        Source  "Oracle.DataAccess" string
                        StackTrace  "   at Oracle.DataAccess.Client.OracleDataReader.GetValue(Int32 i)" string
            -       TargetSite  {System.Object GetValue(Int32)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}
            +       [System.Reflection.RuntimeMethodInfo]   {System.Object GetValue(Int32)} System.Reflection.RuntimeMethodInfo
            -       base    {System.Object GetValue(Int32)} System.Reflection.MemberInfo {System.Reflection.RuntimeMethodInfo}
            +       CustomAttributes    Count = 0   System.Collections.Generic.IEnumerable<System.Reflection.CustomAttributeData> {System.Collections.ObjectModel.ReadOnlyCollection<System.Reflection.CustomAttributeData>}
            +       DeclaringType   {Name = "OracleDataReader" FullName = "Oracle.DataAccess.Client.OracleDataReader"}  System.Type {System.RuntimeType}
                        MemberType  Method  System.Reflection.MemberTypes
                        MetadataToken   100665607   int
            +       Module  {Oracle.DataAccess.dll} System.Reflection.Module {System.Reflection.RuntimeModule}
                    Name    "GetValue"  string
            +       ReflectedType   {Name = "OracleDataReader" FullName = "Oracle.DataAccess.Client.OracleDataReader"}  System.Type {System.RuntimeType}
                        Attributes  FamANDAssem | Family | Virtual | HideBySig  System.Reflection.MethodAttributes
                        CallingConvention   Standard | HasThis  System.Reflection.CallingConventions
                        ContainsGenericParameters   false   bool
                        IsAbstract  false   bool
                        IsAssembly  false   bool
                        IsConstructor   false   bool
                        IsFamily    false   bool
                        IsFamilyAndAssembly false   bool
                        IsFamilyOrAssembly  false   bool
                        IsFinal false   bool
                        IsGenericMethod false   bool
                        IsGenericMethodDefinition   false   bool
                        IsHideBySig true    bool
                        IsPrivate   false   bool
                        IsPublic    true    bool
                        IsSecurityCritical  true    bool
                        IsSecuritySafeCritical  true    bool
                        IsSecurityTransparent   false   bool
                        IsSpecialName   false   bool
                        IsStatic    false   bool
                        IsVirtual   true    bool
            +       MethodHandle    {System.RuntimeMethodHandle}    System.RuntimeMethodHandle
                        MethodImplementationFlags   IL  System.Reflection.MethodImplAttributes
            +       Non-Public members      
            +       Static members      
            +       Non-Public members  

另外,请注意DataReader记录包含值

-       dataReader  {Oracle.DataAccess.Client.OracleDataReader} System.Data.IDataReader {Oracle.DataAccess.Client.OracleDataReader}
    +       [Oracle.DataAccess.Client.OracleDataReader] {Oracle.DataAccess.Client.OracleDataReader} Oracle.DataAccess.Client.OracleDataReader
            Depth   0   int
            IsClosed    false   bool
            RecordsAffected -1  int
    -       Results View    Expanding the Results View will enumerate the IEnumerable   
        -       [0] {System.Data.Common.DataRecordInternal} object {System.Data.Common.DataRecordInternal}
        -       base    {System.Data.Common.DataRecordInternal} System.Data.Common.DbDataRecord {System.Data.Common.DataRecordInternal}
                FieldCount  9   int
                FieldCount  9   int

最初数据记录中的第一项是- _values {object[9]} object[] [0] 0 object {decimal} [1] 0 object {decimal} [2] 0 object {decimal} [3] 0 object {decimal} [4] 1502 object {decimal} + [5] {} object {System.DBNull} + [6] {} object {System.DBNull} + [7] {5/5/2015 12:00:00 AM} object {System.DateTime} + [8] {4/30/2015 12:00:00 AM} object {System.DateTime} + [1] {System.Data.Common.DataRecordInternal} object {System.Data.Common.DataRecordInternal} + [2] {System.Data.Common.DataRecordInternal} object {System.Data.Common.DataRecordInternal} 类型,我认为错误可能是因为这个原因。所以我更改了列序号以将类型DateTime的记录作为第一项 - 但仍然抛出异常。

我认为必须使用int方法做一些事情,但不确定。

任何人都知道可能出现的问题和任何建议/帮助?

对于长代码/例外示例感到抱歉,但不要错过任何详细信息......

2 个答案:

答案 0 :(得分:2)

GetValue应该为空值返回DBNull但是尝试这个

if (!dataReader.isDBNull(i)) 
{
    ...
}

SqlDataReader.IsDBNull

或尝试此语法

if (!DBNull.Value.Equals(dataReader.GetValue(i))) 

答案 1 :(得分:1)

我有同样的例外,因为我在调用 table.FLTT { clear:both; width: 100%; margin: 0 auto; border-collapse: separate; border-spacing: 0; text-decoration: none; background: #fff; border:1px solid #dcdcdc; border-bottom:none;} table.FLTT th { background-color: #4D4D4d!important; color: #FFFFFF!important; padding: 10px 10px; text-decoration: none; transition: 0.0s; font-size: 13px; font-weight:normal; } table.FLTT td { padding: 5px 10px; border-bottom: 1px solid #dcdcdc; text-decoration: none; } table.FLTT tbody > tr:nth-child(odd) > td { background-color: #cccccc; } table.FLTT tbody tr:hover > td { background-color: #222222; color: #F0C237; } table.FLTT tr { text-decoration: none; -webkit-transition:all .5s ease; -moz-transition:all .5s ease; -o-transition:all .5s ease; transition:all .5s ease;} table.FLTT tr a { text-decoration: none; outline:none; color: #005689; } table.FLTT tr:hover a{ background-color: #222222; color:#F0C237; text-decoration:underline; } table.FLTT tr.cat-list-row1 { background:#eee} table.FLTT thead th { color:#fff!important; background:#4D4D4d!important;} 之前忘记调用reader.Read()方法。 也许它有助于某人。