DataTables - (ADO.NET Connection)
DataTables is a class in the ADO.NET library that represents a collection of rows and columns, similar to a database table. It allows you to manipulate data in a table-like structure and provides methods for querying and modifying data in the table.
Syntax
The syntax for creating a DataTable object in C# is as follows:
// Create a new DataTable object
DataTable dt = new DataTable("MyTable");
// Add columns to the table
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Salary", typeof(decimal));
// Add data to the table
dt.Rows.Add(1, "John Doe", 50000.00);
dt.Rows.Add(2, "Jane Smith", 60000.00);
dt.Rows.Add(3, "Bob Johnson", 70000.00);
Example
Here is a simple example that demonstrates how to use a DataTable object to store and display data:
// Create a new DataTable object
DataTable dt = new DataTable("Employees");
// Add columns to the table
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Salary", typeof(decimal));
// Add data to the table
dt.Rows.Add(1, "John Doe", 50000.00);
dt.Rows.Add(2, "Jane Smith", 60000.00);
dt.Rows.Add(3, "Bob Johnson", 70000.00);
// Display the data in a console window
foreach (DataRow row in dt.Rows)
{
Console.WriteLine("{0}\t{1}\t{2:C}", row["ID"], row["Name"], row["Salary"]);
}
Output
The output of the above code will be:
1 John Doe $50,000.00
2 Jane Smith $60,000.00
3 Bob Johnson $70,000.00
Explanation
A DataTable object represents a collection of rows and columns. Each column is represented by a DataColumn object, and each row is represented by a DataRow object. You can add columns and rows to the table using the DataTable.Columns and DataTable.Rows properties, respectively. Once the data has been added to the table, you can query and manipulate it using LINQ or other methods.
Use
DataTables are used extensively in ADO.NET applications for storing and manipulating data. They are particularly useful for storing data that is retrieved from a database or other data source. You can use a DataTable object to represent the data in a database table, and then manipulate it in memory before writing the changes back to the database. DataTables are also commonly used in data binding scenarios, where the data is displayed in user interfaces such as forms or data grids.
Important Points
- A DataTable object represents a collection of rows and columns, similar to a database table.
- You can add columns and rows to the table using the DataTable.Columns and DataTable.Rows properties, respectively.
- Once the data has been added to the table, you can query and manipulate it using LINQ or other methods.
- DataTables are used extensively in ADO.NET applications for storing and manipulating data.
- DataTables are particularly useful for storing data that is retrieved from a database or other data source.
Summary
In this page, we discussed DataTables in ADO.NET. We covered the syntax, example, output, explanation, use, and important points of DataTables. DataTables are a crucial component of ADO.NET, providing a means of representing, storing, and manipulating data in a table-like structure. They are widely used in database-driven applications and data binding scenarios.