将数组初始化为动态大小 - C#

发布于 2024-10-20 02:39:49 字数 797 浏览 2 评论 0原文

我正在使用一个数组来保存 SQLite 查询的结果,目前我正在使用二维表来执行此操作。

我遇到的问题是,我必须在使用数组之前手动指定其中一个索引的大小,但这感觉很浪费,因为我不知道将返回多少行。我确实使用了一个属性,它允许我检索在数组初始化中使用的存在的列数。

示例:

using (SQLiteDataReader dr = cmd.ExecuteReader())
{
       results = new object[10,dr.FieldCount];

       while (dr.Read())
       {
            jIterator++;

            for (int i = 0; i < dr.FieldCount; i++)
            {
                    results[jIterator, i] = dr.GetValue(i);
            }
       }
}
//I know there are a few count bugs

存储示例:

Array content

我只需添加数据只要 while 循环返回 true,就将其添加到数组中,在本例中,我将第一个索引设置为 10,因为我已经知道数据库中有多少元素,并且 10 就足够了。

我将如何更改数组,使其大小可以是动态的,根据我获取数据库结果的方式,这是否可能?

I am using an array to hold the results of an SQLite query, at the moment I am using a two dimensional table to do this.

The problem that I have is that I have to manually specify the size of one of the indexes before the array can be used but it feels like a waste as I have no real knowledge of how many rows will be returned. I do use a property which allows me to retrieve the amount of columns that are present which is used in the arrays initialisation.

Example:

using (SQLiteDataReader dr = cmd.ExecuteReader())
{
       results = new object[10,dr.FieldCount];

       while (dr.Read())
       {
            jIterator++;

            for (int i = 0; i < dr.FieldCount; i++)
            {
                    results[jIterator, i] = dr.GetValue(i);
            }
       }
}
//I know there are a few count bugs

Example Storage:

Array contents

I simply add the data to the array for as long as the while loop returns true, in this instance I set the first index to 10 as I already know how many elements are in the database and 10 will be more than enough.

How would I go about changing the array so it's size can be dynamic, Is this even possible based on the way I am getting the database results?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遇见了你 2024-10-27 02:39:49

您不应该使用数组,而应该使用动态大小的容器,例如 List。或者,更好的是,您可以使用 ADO.NET DataTable 之类的东西,因为这正是它设计用于存储的内容,并且使用 DbDataAdapter 将避免重复到处都是相同的 IDataReader 代码。

You should not be using an array, you should be using a dynamically-sized container like List<T>. Or, better yet, you could use something like the ADO.NET DataTable, since this is exactly what it's designed to store, and using a DbDataAdapter will avoid having to repeat the same IDataReader code all over the place.

酷到爆炸 2024-10-27 02:39:49

不要使用数组。使用通用列表System.Collections.Generic.List,您甚至可以拥有一个列表的列表。这些是可调整大小的,并为您完成所有这些管道工作。

Don't use an array. Use a generic list System.Collections.Generic.List<type>, you can even have a List of a List. These are resizable and do all of that plumbing for you.

无人问我粥可暖 2024-10-27 02:39:49

我建议定义一个结构来保存您的联系信息,然后创建该类型的通用列表。它们可以无限制地扩展。

I would suggest defining a structure to hold your contact information, then just creating a generic list of that type. They can expand without limit.

不打扰别人 2024-10-27 02:39:49

您有多种选择:

  • 使用 List 而不是二维数组。
  • 将数据读取器加载到数据集而不是数组中
  • 完全跳过数据读取器并使用数据适配器来填充数据集
  • 使用迭代器块将数据读取器转换为 IEnumerable 而不是数组

其中,在大多数情况下,到目前为止,您最好的选择是最后的;这意味着您不需要一次将查询的整个结果集都存储在内存中,这是首先使用 SqlDataReader 的要点。

You have several options:

  • Use a List<T[]> instead of a 2 dimensional array.
  • Load your datareader into a dataset instead of an array
  • Skip the datareader entirely and use a dataadapter to fill a dataset
  • Use an iterator block to transform the datareader into an IEnumerable instead of an array

Of these, in most cases by far your best option is the last; it means you don't need to have the entire result set of your query in memory at one time, which is the main point for using an SqlDataReader in the first place.

梦在深巷 2024-10-27 02:39:49

您可以在 C# 中创建动态数组。

int[] intarray;

尽管其他建议列出的回复可能更合适。

You can create dynamic arrays in C#.

int[] intarray;

Although other responses that suggest List may be more appropriate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文