Explanation of C DataTable

  • 2021-09-11 21:00:23
  • OfStack

DataTable is often used in projects. Assuming that DataTable is used properly, it can not only make the program concise and useful, but also improve the performance and achieve twice the result with half the effort. Now, the use skills of DataTable are summarized.

1. Add a reference


using System.Data;

2. Create a table


// Create 1 Empty table 
DataTable dt = new DataTable();
// Create 1 A person named "Table_New" Empty table of 
DataTable dt = new DataTable("Table_New");

3. Create a column


//1. Create an empty column 
DataColumn dc = new DataColumn();
dt.Columns.Add(dc);
//2. Create a column with column name and type name ( Choose either way 1)
dt.Columns.Add("column0", System.Type.GetType("System.String"));
dt.Columns.Add("column0", typeof(String));
//3. Adding Columns through Column Schema 
DataColumn dc = new DataColumn("column1",System.Type.GetType("System.DateTime"));
DataColumn dc = new DataColumn("column1", typeof(DateTime));
dt.Columns.Add(dc);

4. Create a row


//1. Create a blank line 
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
//2. Create a blank line 
dt.Rows.Add();
//3. Create and assign values through the row frame 
dt.Rows.Add(" Zhang 3",DateTime.Now);//Add The data order of the parameters in it should be the same as that of the dt The order corresponds to the columns in the  
//4. By copying dt2 Of a table 1 Row to create 
dt.Rows.Add(dt2.Rows[i].ItemArray);

5. Assignment and value taking


// Assignment of new construction bank 
DataRow dr = dt.NewRow();
dr[0] = " Zhang 3";// Assignment by Index 
dr["column1"] = DateTime.Now; // Assign by name 
// Assign values to existing rows of the table 
dt.Rows[0][0] = " Zhang 3"; // Assignment by Index 
dt.Rows[0]["column1"] = DateTime.Now;// Assign by name 
// Value 
string name=dt.Rows[0][0].ToString();
string time=dt.Rows[0]["column1"].ToString();

6. Filter rows


// Select column1 Collection of rows with empty column values 
DataRow[] drs = dt.Select("column1 is null");
// Select column0 Column value is " Li 4" A collection of rows of 
DataRow[] drs = dt.Select("column0 = ' Li 4'");
// Screening column0 Column values have " Zhang " A collection of rows of ( Fuzzy query )
DataRow[] drs = dt.Select("column0 like ' Zhang %'");// If multiple criteria are filtered, you can add  and  Or  or
// Screening column0 Column values have " Zhang " And press the collection of rows of column1 Descending sort 
DataRow[] drs = dt.Select("column0 like ' Zhang %'", "column1 DESC");

7. Delete rows


// Use DataTable.Rows.Remove(DataRow) Method 
dt.Rows.Remove(dt.Rows[0]);
// Use DataTable.Rows.RemoveAt(index) Method 
dt.Rows.RemoveAt(0);
// Use DataRow.Delete() Method 
dt.Row[0].Delete();
dt.AcceptChanges();

//----- Differences and points for attention -----
//Remove() And RemoveAt() The method is to delete directly 
//Delete() Method simply marks the row as deleted , but it still exists, and it can be DataTable.RejectChanges() Rollback, causing the row to be undeleted. 
// Use Rows.Count To get the number of rows, or to delete the number of rows before, you need to use DataTable.AcceptChanges() Method to commit the changes. 
// If you want to delete DataTable Multiple lines in, you should use reverse loop DataTable.Rows , and it can't be used foreach Cyclic deletion, because the index will change when deleting in positive order, and the program will be abnormal, so it is difficult to predict the consequences. 
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
    dt.Rows.RemoveAt(i);
}

8. Copy tables


// Copy the table, copying the table structure and the data in the table at the same time 
DataTable dtNew = new DataTable();
dtNew = dt.Copy();
// Copy table 
DataTable dtNew = dt.Copy(); // Duplicate dt Table data structure 
dtNew.Clear() // Empty data 
for (int i = 0; i < dt.Rows.Count; i++)
{
  if ( Conditional statement )
  {
     dtNew.Rows.Add(dt.Rows[i].ItemArray); // Add data rows 
  }
}
// Cloning a table, only copying the table structure, excluding data 
DataTable dtNew = new DataTable();
dtNew = dt.Clone();
// If you only need a certain in a certain table 1 Row 
DataTable dtNew = new DataTable();
dtNew = dt.Copy();
dtNew.Rows.Clear();// Empty table data 
dtNew.ImportRow(dt.Rows[0]);// This is joined by the first 1 Row 

9. Table sorting


DataTable dt = new DataTable();// Create a table 
dt.Columns.Add("ID", typeof(Int32));// Add Column 
dt.Columns.Add("Name", typeof(String));
dt.Columns.Add("Age", typeof(Int32));
dt.Rows.Add(new object[] { 1, " Zhang 3" ,20});// Add Row 
dt.Rows.Add(new object[] { 2, " Li 4" ,25});
dt.Rows.Add(new object[] { 3, " Wang 5" ,30});
DataView dv = dt.DefaultView;// Get table view 
dv.Sort = "ID DESC";// According to ID Reverse sort 
dv.ToTable();// Conversion table 

The above is the use of C # DataTable, and I hope it will be helpful for everyone to learn C # programming.


Related articles: