SoFunction
Updated on 2025-03-10

Detailed explanation of the differences between DataSet, DataTable, and DataRow

DataSet
Indicates the cache of data in memory.

property
Tables Gets a collection of tables contained in the DataSet.

["sjxx"]

 

DataTable

A table representing data in memory.

Public properties
Columns Gets a collection of columns belonging to the table.

DataSet Gets the DataSet to which this table belongs.

DefaultView Gets a custom view of a table that may include filtered views or cursor locations.

PrimaryKey Gets or sets an array of columns that act as the primary key of the data table.

Rows Gets a collection of rows belonging to the table.

TableName Gets or sets the name of the DataTable.

 

DataRow

Represents a row of data in the DataTable

row["index"]

 

DataColumn

Represents the schema of the column in the DataTable.


Copy the codeThe code is as follows:

Common operation examples of DataTable and DataSet

//Create DataSet
DataSet ds = new DataSet();

//Create DataTable
DataTable dt = new DataTable();
("id",("System.Int32"));
["id"].AutoIncrement = true;
("name",(""));

//Insert row
DataRow dw1 = ();
dw1["name"] = "test1";
(dw1);

DataRow dw2 = ();
dw2["name"] = "test2";
(dw2,0);

//Add DataTable to DataSet
(dt);

//Query in DataTable
DataTable dt = new DataTable();
DataRow dr[] = ("1 = 1");

//DataTable update
DataTable dt = (DataTable)["MYCACHE"];
DataRow[] dr = ("1 = 1");
if ( > 0)
{
    dr[0]["colName"] = "colValue";
}

//statistics
object o = ("SUM(col_name)", "1=1");