What is the best way to create a DataTable with the same structure as a table in my SqlServer database? At present, I am using SqlDataAdapter.Fill() with a query that brings back the columns but no rows. That's works fine, but it seems klutzy.
Is there a better way?
Well, if you use Linq2Sql, you can reflect over the entity class, and create the datatable based on the properties name and datatype.
Type type = typeof(Product); //or whatever the type is
DataTable table = new DataTable();
foreach(var prop in type.GetProperties())
{
table.Columns.Add(prop.Name, prop.PropertyType);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With