The DataTable is a central object in the ADO.NET library. Other objects that use the DataTable include the DataSet and DataView.
When accessing DataTable Object, note that they are conditionally case sensitive.
If you are creating a DataTable programmatically, you must first define its schema by adding DataColumn objects to the DataColumnCollection.
To add rows to a DataTable, you must first use the NewRow method to return a new DataRow object. The NewRow method returns a row with the schema of the DataTable, as it is defined by the table’s DataColumnCollection.
The maximum number of rows that a DataTable can store is 16,777,216.
How to use Datatable in C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | static void Main(string[] args) { using (DataTable dt = new DataTable("Test")) { dt.Columns.Add("ID",typeof(int)); dt.Columns.Add("FirstName",typeof(string)); dt.Columns.Add("LastNameName",typeof(string)); dt.Columns.Add("Date",typeof(DateTime)); dt.Rows.Add(1, "Marc", "Sun", "19.03.1999"); dt.Rows.Add(2, "Mike", "Mon", "20.08.2000"); dt.Rows.Add(3, "Bob", "Tue", "11.11.2011"); dt.Rows.Add(4, "Eric", "Red", DateTime.Now); foreach (DataRow dr in dt.Rows) { Console.WriteLine("ID : {0}\tFirst Name : {1} \t Last Name : {2} \t Date : {3}", dr[0], dr[1], dr[2], dr[3]); } } Console.ReadKey(); } |
Output: