When DataTable is used today, some common knowledge in DataTable is recorded for future reference.
For this reason, I wrote a very easy example. The example is prepared for our rookie.
I would like to give more advice on the shortcomings in the examples,
Includes:
1: Create a able
2: How to Use
3: How to Use foreach
Static void Main (string [] args)
{
DataTable dt = InitialData ();
// Find the column whose value is greater than A_3 in able
DataRow [] drs = dt. Select ("A> 'A _ 3'", "A desc ");
Console. WriteLine ("A \ tB \ tC \ tD ");
// Traverse data
Foreach (DataRow row in drs)
{
String a = row ["A"] as string;
String B = row ["B"] as string;
String c = row ["C"] as string;
String d = row ["D"] as string;
Console. WriteLine (a + "\ t" + B + "\ t" + c + "\ t" + d );
}
Console. ReadKey ();
}
/// <Summary>
/// Initialize Test Data
/// </Summary>
Public static DataTable InitialData ()
{
// Create a data table named TestTable
DataTable dt = new DataTable ("TestTable ");
// Create four columns for the table: A, B, C, and D
Dt. Columns. Add (new DataColumn (""));
Dt. Columns. Add (new DataColumn ("B "));
Dt. Columns. Add (new DataColumn ("C "));
Dt. Columns. Add (new DataColumn ("D "));
For (int I = 1; I <= 10; I ++)
{
// Add a new data row and add data
DataRow dr = dt. NewRow ();
Dr ["A"] = "A _" + I;
Dr ["B"] = "B _" + I;
Dr ["C"] = "C _" + I;
Dr ["D"] = "D _" + I;
// Add this row to the able
Dt. Rows. Add (dr );
}
Return dt;
}
Author: "The Soul of Wolf"