C# 解释ds.Tables[0].Rows.Count?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14023085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:26:45  来源:igfitidea点击:

Explain ds.Tables[0].Rows.Count?

c#asp.net.net

提问by Arbaaz

I am a beginner, so don't be amused at my questions. I think if (ds.Tables[0].Rows.Count > 0)is used to check if the dataset is empty. But what exactly [0]signify in this case? Can you explain this statement in a little more detail? And this one too.. ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());

我是初学者,所以不要被我的问题逗乐。我认为 if (ds.Tables[0].Rows.Count > 0)用于检查数据集是否为空。但[0]在这种情况下究竟意味着什么?你能更详细地解释一下这个说法吗?还有这个.. ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());

采纳答案by Dave Zych

It gives you access to the first table of the DataSet. A DataSetholds an array of DataTables, and it can have 0, 1 or many of these DataTables. You access them just like any other array - by indexing into them.

它使您可以访问DataSet. ADataSet持有 的数组DataTables,它可以有 0、1 或许多这样的DataTables。您可以像访问任何其他数组一样访问它们 - 通过对它们进行索引。

If there were 2 DataTablesin this DataSet, you could access the first one by using ds.Tables[0], and the second one by ds.Tables[1]

如果有2DataTables在此DataSet,您可以访问使用的第一个ds.Tables[0]和第二个通过ds.Tables[1]

The ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());statement is adding a new row to the first DataTablein the DataSet. By calling ds.Tables[0].NewRow(), you are creating a new row that is associated to the first DataTablein the array.

ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());语句添加新行到第一个DataTableDataSet。通过调用ds.Tables[0].NewRow(),您正在创建一个DataTable与数组中的第一行相关联的新行。

回答by Ashwin Singh

There can be more than one table in dataset, by using ds.Tables[0] you are taking the first table.

数据集中可以有多个表,通过使用 ds.Tables[0] 您正在获取第一个表。

回答by Ulises

Here dsis an instance of DataSet. A DataSet may contain multiple instances of Table.

dsDataSet 的一个实例。一个 DataSet 可能包含多个Table实例。

ds.Tables[0]is accessing the first tablein the Tablescollection. And ds.Tables[0].Rows.Countis counting the rows on that first table.

ds.Tables[0]是访问的第一个表Tables收集。并且ds.Tables[0].Rows.Count正在计算第一个表上的行数。

Rows accesses the DataRow collection. The Add method creates one more row; however, you need to pass an instance of DataRow as the parameter. This DataRow object must have the same structure (columns)that the table, for this reason, you use the same table to create a new DataRoq: ds.Tables[0].NewRow()

Rows 访问 DataRow 集合。Add 方法再创建一行;但是,您需要传递一个 DataRow 实例作为参数。此 DataRow 对象必须具有与表相同的结构(列),因此,您使用相同的表来创建新的 DataRoq:ds.Tables[0].NewRow()

回答by Kishore Kumar

ds >>>>>>>>>>> Object of DataSet class from System.Data namespace
Tables[0] >>>> DataTable class object at index 0 inside ds object
Rows >>>>>>>>> RowCollection object which contain all the rows
Count >>>>>>>> Used to count no of rows inside the collection.

回答by dekdev

all above explanation is right .

以上解释都是对的。

but here are my 2 cents

但这是我的 2 美分

ds.Tables[0].Rows.Count

ds.Tables[0].Rows.Count

before you use above statement , it's good if you check below condition

在使用上述语句之前,最好检查以下条件

if (ds !=null && ds.Tables.count>0 )