System.Data.DataTable、DataRowCollection 和 DataColumns:索引如何工作?
在 DataTable
中:
我可以像这样访问所有 DataRow
元素:
DataTable table = GetMyTable();
for (int i = 0; i < table.Rows.Count; i++) {
DataRow row = table.Rows[i];
Console.WriteLine(row);
}
访问所有 DataColumn
元素:
DataTable table = GetMyTable();
for (int i = 0; i < table.Columns.Count; i++) {
DataColumn column = table.Columns[i];
Console.WriteLine(column);
}
另外,我可以像这样 ,我可以像这样访问 DataTable
对象的每个单独的 Cell
:
DataTable table = GetMyTable();
for (int i = 0; i < table.Rows.Count; i++) {
DataRow row = table.Rows[i];
object[] array = row.ItemArray;
for (int j = 0; j < array.Length; j++) {
object cell = array[j];
Console.WriteLine(cell);
}
}
至少看起来第三种技术丢失了一些信息。
这是我的问题: 如何管理这个对象数组?
我一直在研究如何使用这种基本类型的功能重新创建某些东西(因为我已经熟悉它了),但我只是不知道如何设计所有这些小子类以使其发挥作用!
In a DataTable
:
I can access all of the DataRow
elements like so:
DataTable table = GetMyTable();
for (int i = 0; i < table.Rows.Count; i++) {
DataRow row = table.Rows[i];
Console.WriteLine(row);
}
Also, I can access all of the DataColumn
elements like this:
DataTable table = GetMyTable();
for (int i = 0; i < table.Columns.Count; i++) {
DataColumn column = table.Columns[i];
Console.WriteLine(column);
}
Finally, I can access each individual Cell
of the DataTable
object like this:
DataTable table = GetMyTable();
for (int i = 0; i < table.Rows.Count; i++) {
DataRow row = table.Rows[i];
object[] array = row.ItemArray;
for (int j = 0; j < array.Length; j++) {
object cell = array[j];
Console.WriteLine(cell);
}
}
At least it looks like some information is lost in the third technique.
Here's my question:
How is this array of objects being managed?
I've been playing with how to recreate something with this basic type of functionality (since I am already familiar with it), but I just don't know how to design all these little sub-classes to make it work!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
建立一个简单的类关系并不困难:
Making a simple class relationship isn't to difficult:
该表有一个
DataColumn
列表。这是列定义的列表。该表有一个行列表。列列表中的每一行对应于每一列一个单元格。诀窍在于行是由表创建的。该表知道
Columns
集合的长度,以及列的数据类型和它们的任何约束。当表创建DataRow
时,它会使用正确的列数创建它。The table has a list of
DataColumn
. This is a list of column definitions. The table has a list of rows. Each row has one cell for each column in the list of columns.The trick is that the rows are created by the table. The table knows the length of the
Columns
collection, as well as the data types of the columns and any constraints on them. When the table creates aDataRow
, it creates it with the correct number of columns.它可以像这样简单:
它可以让您像这样访问它:
在实际的 DataTable 中,object[] 被包装在 DataRow 类中,该类提供了诸如
Field<>
等访问功能,而List<>
通过DataRowCollection
向外部暴露了更有限的信息量班级。It can be as simple as:
Which lets you access it like:
In the actual
DataTable
,object[]
is wrapped in aDataRow
class that provides access functions likeField<>
and such, andList<>
exposes a more limited amount of information to the outside through theDataRowCollection
class.