DataReader获取结果集架构信息
打开DataReader时,可以使用GetSchemaTable方法检索有关当前结果集的架构信息。
SqlCommand command = new SqlCommand( "SELECT CategoryID, CategoryName FROM Categories;", connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
DataTable schemaTable = reader.GetSchemaTable();
foreach (DataRow row in schemaTable.Rows)
{ foreach (DataColumn column in schemaTable.Columns)
{
Console.WriteLine(String.Format("{0} = {1}",
column.ColumnName, row[column]));
}
}