Question - What is DataTable in ADO.NET?
Answer -
DataTable in ADO.NET represents a single table in a DataSet that has in-memory relational data. The data within DataTable is local to the .NET framework-based application to which it belongs but can be populated using a DataAdapter from different data sources such as Microsoft SQL Server. The DataTable class belongs to the System.Data namespace within the library of .NET Framework.
DataTable can be represented in .aspx.cs code as given below:
protected void DataTableExample()
{
SqlConnection conn = new SqlConnection("Write the database connection string");
conn.Open();
SqlCommand cd = new SqlCommand("Write the query or procedure", conn);
SqlDataAdapter d = new SqlDataAdapter(cd);
DataTable dt = new DataTable();
d.Fill(dt);
grid.DataSource = dt;
grid.DataBind();
}
The SQL connection and SQL command object will be created. We pass the SQL query to the object of the SQL command class. A new data table object will be created by using the DataTable class and it is filled with data using a data adapter.