DataTable, DataSet

DataTable - центрльний об'єкт в бібліотеці ADO.NET 

Вивести перший стовпчик

1.

foreach (DataRow row in table.Rows){ 

     Console.WriteLine(row.Field<String>(0)); 

2.

for (int i = 0; i < table.Rows.Count; i++)

{

     Console.WriteLine(table.Rows[i]["Cliet"]);

     //Console.WriteLine(table.Rows[i][0]);

}

Вивести усі стовпчики, які є в таблиці

foreach (DataRow row in table.Rows)

{

     var cells = row.ItemArray;

     foreach (object cell in cells)

     {

          Console.Write(cell + " ");

     }

     Console.WriteLine();

}

Назви стовпчиків таблиці

foreach (DataColumn column in table.Columns)

{

     Console.WriteLine(column.ColumnName);

}

Змінні

table.Rows.Count - кількість рядків

table.Columns.Count - кількість колонок

table.TableName - назва таблиці

Функції

table.Clear() - очистити

table.Rows.Add("1", "2", "3", "4")  - додати рядок (не вихидити за Columns.Count)

table.Columns.Add("Bool") - додати колонку з назвою "Bool"

table.Rows[0][2] = 300;

table.Rows[1]["Amount"] = 300;

Видалити 

1.

table.Rows.RemoveAt(1);

2.

DataRow row = table.Rows[0];

table.Rows.Remove(row);

Сортувати

table.DefaultView.Sort = "Date ASC";       // по зростанню

table.DefaultView.Sort = "[Date] DESC";  // можна в [], по спаданню

table = table.DefaultView.ToTable();

Копіювати

1.

DataTable dt_copy = new DataTable();

dt_copy = table.DefaultView.ToTable();

2.

DataTable dt_copy = new DataTable();

dt_copy = dt.Copy();

Створити таблицю

1.

DataTable table = new DataTable("Table1");

DataColumn column1 = new DataColumn("Date", Type.GetType("System.String"));

DataColumn column2 = new DataColumn("Cliet", Type.GetType("System.String"));

DataColumn column3 = new DataColumn("Amount", Type.GetType("System.String"));

DataColumn column4 = new DataColumn("Ground", Type.GetType("System.String"));

table.Columns.Add(column1);

table.Columns.Add(column2);

table.Columns.Add(column3);

table.Columns.Add(column4);

table.Rows.Add(new object[] { "12.12.2019", "Ronaldo", "900", "Check" });

table.Rows.Add(new object[] { "10.10.2019", "Messi", "800", "payment on receipt of the goods" });

table.Rows.Add(new object[] { "11.11.2019", "Lampard", "700", "Cash withdrawal ATM" });

2.

DataTable table = new DataTable("Table1");

DataColumn column1 = new DataColumn("Id", Type.GetType("System.Int32"));

column1.Unique = true; // має бути унікальним

column1.AllowDBNull = false; // не може бути null

column1.AutoIncrement = true; // буде збільшуватися 

column1.AutoIncrementSeed = 1; // початкове значення

column1.AutoIncrementStep = 1; // збільшувати на 1

DataColumn column2 = new DataColumn("Name", Type.GetType("System.String"));

DataColumn column3 = new DataColumn("Price", Type.GetType("System.Decimal"));

column3.DefaultValue = 100; // типове значення

DataColumn column4 = new DataColumn("Discount", Type.GetType("System.Decimal"));

column4.Expression = "Price * 0.2"; // формула для клатинки

table.Columns.Add(column1);

table.Columns.Add(column2);

table.Columns.Add(column3);

table.Columns.Add(column4);

table.PrimaryKey = new DataColumn[] { table.Columns["Id"] }; // первинний ключ

table.Rows.Add(new object[] { null, "C#", 100 }); // додати рядок

3.

Object[] rows = {

     new Object[]{ "12.12.2021", "Яна", 900, "Check" },

     new Object[]{ "10.10.2019", "", 800, "payment on receipt of the goods" },

     new Object[]{ "11.11.2070", "Lampard", 7000, "Cash withdrawal ATM" },

};

foreach (Object[] row in rows)

{

      table.Rows.Add(row);

}