using System.Data; using System.IO; // Load a data table from a CSV file DataTable table = new DataTable(); using (StreamReader reader = new StreamReader("data.csv")) { table.Load(reader); } // Access the data in the table foreach (DataRow row in table.Rows) { foreach (DataColumn col in table.Columns) { Console.Write(row[col] + "\t"); } Console.WriteLine(); }
using System.Data; using System.Data.SqlClient; // Load a data table from a SQL Server database DataTable table = new DataTable(); using (SqlConnection conn = new SqlConnection("connection string")) { SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", conn); conn.Open(); table.Load(cmd.ExecuteReader()); } // Access the data in the table foreach (DataRow row in table.Rows) { foreach (DataColumn col in table.Columns) { Console.Write(row[col] + "\t"); } Console.WriteLine(); }In both examples, we create a new empty data table and then use the `Load` method to populate it with data. We can then access the data in the table through its `Rows` and `Columns` properties. The `Load` method can handle various data sources and formats, making it a versatile tool for data processing in C#.