简体   繁体   中英

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

I am a beginner, so don't be amused by 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());

It gives you access to the first table of the DataSet . A DataSet holds 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.

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

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

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

ds.Tables[0] is accessing the first table in the Tables collection. And ds.Tables[0].Rows.Count is counting the rows on that first table.

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()

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.

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

all above explanation is right .

but here are my 2 cents

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 )  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM