SQL Datareader 不保存值

发布于 2024-12-16 18:57:06 字数 3450 浏览 2 评论 0原文

过去 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. 可以采取什么措施来解决这个问题?

I have been working with a project for last 4 months. We are using a custom framework for the development. The problem I am talking about, was working for all other classes. But for the first time I am facing this weird incident. Now Straight to break point.

My framework code is like

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;
            }
        }

In the above method the dataReader is sent to Maps method.

The Maps method is ......

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;
    }

The SQLNullHandler is given below:

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
  }

The Funny thing is for all these classes these methods and lines of code work very fine, but in this scenario after the line SQLNullHandler nullHandler = new SQLNullHandler(theReader); the datareder becomes empty.
My questions are

  1. Why is this Happening and next,
  2. what can be done to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文