将数组初始化为动态大小 - C#
我正在使用一个数组来保存 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
存储示例:
我只需添加数据只要 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不应该使用数组,而应该使用动态大小的容器,例如
List
。或者,更好的是,您可以使用 ADO.NETDataTable
之类的东西,因为这正是它设计用于存储的内容,并且使用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.NETDataTable
, since this is exactly what it's designed to store, and using aDbDataAdapter
will avoid having to repeat the sameIDataReader
code all over the place.不要使用数组。使用通用列表
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.我建议定义一个结构来保存您的联系信息,然后创建该类型的通用列表。它们可以无限制地扩展。
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.
您有多种选择:
List
而不是二维数组。其中,在大多数情况下,到目前为止,您最好的选择是最后的;这意味着您不需要一次将查询的整个结果集都存储在内存中,这是首先使用 SqlDataReader 的要点。
You have several options:
List<T[]>
instead of a 2 dimensional 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.
您可以在 C# 中创建动态数组。
尽管其他建议列出的回复可能更合适。
You can create dynamic arrays in C#.
Although other responses that suggest List may be more appropriate.