SQL Datareader没有保存值

时间:2011-11-19 06:53:26

标签: c# asp.net sqldatareader

过去4个月我一直在从事一个项目。我们正在使用自定义框架进行开发。我正在谈论的问题是,正在为所有其他课程工作。但这是我第一次遇到这个奇怪的事件。现在直截了当。

我的框架代码就像

public static List<ViewNotSetBillableCoursesEntity> GetAllNotSetBillableCources()
        {
            try
            {
                List<ViewNotSetBillableCoursesEntity> entities = new List<ViewNotSetBillableCoursesEntity>();
                string command = SELECT;
                SqlConnection sqlConnection = MSSqlConnectionHandler.GetConnection();
                SqlDataReader dataReader = QueryHandler.ExecuteSelect(command, sqlConnection);

                entities = Maps(dataReader);
                dataReader.Close();
                return entities;
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

在上面的方法中,dataReader被发送到Maps方法。

Maps方法是......

private static List<ViewNotSetBillableCoursesEntity> Maps(SqlDataReader theReader)
    {
        SQLNullHandler nullHandler = new SQLNullHandler(theReader);
        // the incident is happening here, the SQLNullHandler is given below.
        List<ViewNotSetBillableCoursesEntity> entities = null;
        while (theReader.Read())
        {
            if (entities == null)
            {
                entities = new List<ViewNotSetBillableCoursesEntity>();
            }
            ViewNotSetBillableCoursesEntity entity = Mapper(nullHandler);
            entities.Add(entity);
        }

        return entities;
    }

SQLNullHandler如下:

puplic Class SQLNullHandler
  {
      private IDataReader _reader;
      public SQLNullHandler(IDataReader reader)
        {
            _reader = reader;
        }
      #region Get Null value
    public static object GetNullValue(int Value)
    {
        if(Value==0)
        {
            return null;
        }
        else
        {
            return Value;
        }
    }

    public static object GetNullValue(double Value)
    {
        if (Value == 0)
        { 
            return null; 
        }
        else
        { 
            return Value; 
        }
    }

    public static object GetNullValue(decimal Value)
    {
        if (Value == 0)
        { 
            return null; 
        }
        else
        { 
            return Value; 
        }
    }

    public static object GetNullValue(DateTime Value)
    {
        if(DateTime.MinValue==Value)
        {
            return null;
        }
        else
        {
            return Value;
        }
    }

    public static object GetNullValue(string Value)
    {
        if(Value.Length<=0)
        {
            return null;
        }
        else
        {
            return Value;
        }
    }
    #endregion
      public IDataReader  Reader
    {
        get{return _reader;}
    }
     public bool IsNull(int index)
    {
        return _reader.IsDBNull(index);
    }

    public int GetInt32(int i)
    {
        return _reader.IsDBNull(i)? 0 : _reader.GetInt32(i);
    }

    public byte GetByte(int i)
    {
        return _reader.IsDBNull(i)? (byte)0 : _reader.GetByte(i);
    }
           //and so on for all possible type for this app
  }

有趣的是,对于所有这些类,这些方法和代码行工作得很好,但在这种情况下,行SQLNullHandler之后的nullHandler = new SQLNullHandler(theReader);数据存储器变空。 我的问题是

  1. 为什么会发生此事,接下来,
  2. 可以做些什么来解决这个问题?

0 个答案:

没有答案