Monday, April 6, 2009

C# - ADO.NET - How to generate a DataTable based on table schema

In refer to another blog entry with title of
"Read table schema by SqlDataReader" in this weblog,
http://iborn2code.blogspot.com/2009/04/c-adonet-how-to-get-table-schema.html
now the following code shows how to use the
table schema to generate a DataTable based on
the schema:


DataTable retDtaTbl = new DataTable();

if (tblSchema.Rows.Count == 0)
return retDtaTbl;

tblSchema.DefaultView.Sort = "ColumnOrdinal";
foreach (DataRow rowSchema in tblSchema.Rows)
retDtaTbl.Columns.Add(
rowSchema["ColumnName"].ToString(),
System.Type.GetType( rowSchema["DataType"].ToString() ) );

Share/Bookmark

No comments: