【问题标题】:ArrayList capacity does not exceed 32768ArrayList 容量不超过 32768
【发布时间】:2018-03-17 14:31:02
【问题描述】:

我有一个从数据库返回大量行的方法。 (请看下文)

public static ACollection GetListFromDatabase(string customername)
    {
        DBFactory factory = DBFactory.Instance;
        ACollection custcol = new ACollection();
       //This is a collection class extended from System.Collections.CollectionBase

       System.Data.IDataReader reader = null;

        try
        {
            reader = factory.GPDb.ExecuteReader("spGetCustomerInfo", customernumber);
            while (reader.Read())
            {
                ACollection cust = ACollection.ListFromReader(reader); // returns info and assign it to ACollection object.
                custcol.InnerList.Add(cust);
            }
        }
        catch (Exception e)
        {
            String error = e.Message;
        }
        finally
        {
            if (reader != null)
                reader.Close();
        }
        return custcol;
    }

当我运行这个方法时,我意识到 custcol.InnerList 的计数是 32767,它应该在 34000 左右。然后我看到它进入了异常。

错误消息显示“对于 Int16,值太大或太小。”

我相信,这个数组列表的容量以某种方式被分配为 int16。有人可以帮我增加容量吗?

谢谢

编辑: 这是完整的堆栈跟踪

   at System.Convert.ToInt16(Int64 value)
   at System.Int64.System.IConvertible.ToInt16(IFormatProvider provider)
   at System.Convert.ToInt16(Object value)
   at Quest___Shared.CustomerCrossReference.ListFromReader(IDataReader reader) in C:\vsproject\CustomerCrossReference.cs:line 105
   at Quest___Shared.ACollection.GetListFromDatabase(String customernumber) in C:\vsproject\ACollection.cs:line 88

【问题讨论】:

  • 一个 DataTable 可以容纳 16,777,216 行
  • 您确定是ArrayList 的问题吗?您对ACollection.ListFromReader 的实现是否尝试将大的32 位或64 位列值分配给cust 的任何16 位属性?或者它可能正在为ushort 属性分配一个负列值?能够查看异常的整个堆栈跟踪可能有助于确定这一点。
  • 我怀疑你的分析。但是 ArrayList 无论如何都被弃用了。请改用 List

标签: c# arraylist collections


【解决方案1】:

将给您带来问题的变量类型更改为Int32Int64,然后您将能够插入更大或更小的数字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 2023-01-25
    • 2017-08-12
    • 1970-01-01
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多