DataTable - Load from IDataReader - CreateDataReader - ReadXml WriteXml
The DataTable in ADO.NET 2.0 has been seriously beefed
up and I absolutely love it. Not everything we consume deals with multiple resultsets and requires the ever popular
DataSet. With the cool new features in the ADO.NET 2.0 DataTable, we can get many of the cool
features in the DataSet without all the extra baggage and memory footprint particular to multiple resultsets.
Load DataTable from IDataReader
Probably the coolest feature is that the DataTable can now be loaded via a IDataReader
( SqlDataReader for example ) without the need for a DataAdapter ( SqlDataAdapter ).
The DataTable now has a Load method that accepts an IDataReader
as an argument:
DataTable.Load(IDataReader datareader)
Here is an example of loading the DataTable with an IDataReader:
private void Form1_Load(object sender,
EventArgs e)
{
dataGridView1.DataSource = GetData();
}
private DataTable GetData()
{
DataTable dt = new DataTable();
using (SqlConnection connection
= new SqlConnection("ConnectionString"))
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.CommandText = "SELECT * FROM Customers";
connection.Open();
using (SqlDataReader reader =
command.ExecuteReader
(CommandBehavior.CloseConnection))
{
dt.Load(reader);
}
}
return dt;
}
DataTable.CreateDataReader Method
If that wasn't enough, the DataTable also has a CreateDataReader method
that returns a DataTableReader:
public DataTableReader CreateDataReader ()
This means you can use a DataTable as the source for anything that wants a DataTableReader, DbDataReader
or IDataReader. Since the DataTable can be cached and the DataReader cannot, this opens up some extra flexibility in
your data access layer when you decide you need caching at some point.
Below is a bit of a far fetched, but descriptive example of the CreatDataReader
Method of the DataTable. I am manually creating a DataTable, adding myself as a Customer DataRow, and then calling CreateDataReader
to return an IDataReader object. This is then used as input in the DataTable.Load Method of another DataTable (dt),
which is then used as a DataSource to the DataGridView.
private void Form1_Load(object sender,
EventArgs e)
{
DataTable dt =
new DataTable();
dt.Load(GetReader());
dataGridView1.DataSource = dt;
}
private IDataReader GetReader()
{
DataTable
dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Firstname", typeof(string));
dt.Columns.Add("Lastname",
typeof(string));
dt.Rows.Add((new Object[] {"HAYD", "David", "Hayden"}));
return dt.CreateDataReader();
}
DataTable.ReadXml and WriteXml
Like the DataSet, the DataTable now also supports ReadXml, ReadXmlSchema,
WriteXml, and WriteXmlSchema. This is phenomenal when used with CreateDataReader,
etc.
private void Form1_Load(object sender,
EventArgs e)
{
dataGridView1.DataSource = GetDataTable();
}
private DataTable
GetDataTable()
{
DataTable dt
= new DataTable();
dt.ReadXmlSchema("CustomerSchema.xml");
dt.ReadXml("CustomerData.xml");
return dt;
}
Conclusion
The DataTable in ADO.NET 2.0 opens up a whole new world of possibilities when
dealing with a single resultset that doesn't need all the baggage that comes with the DataSet.