Client Interfaces Guide > Connecting to TDV Server through ADO.NET > Sample Code for Testing of an ADO.NET Driver > Getting Column Metadata
 
Getting Column Metadata
The following code shows a way to access column metadata using CompositeDataReader.
You can call GetSchemaTable to get the DataTable object. It contains some rows, and returns every row present with column metadata. The metadata contains:
ColumnName, ColumnOrdinal, ColumnSize, NumericPrecision, NumericScale, DataType, ProviderType, IsLong, AllowDBNull, IsReadOnly, IsRowVersion, IsUnique, IsKey, IsAutoIncrement, BaseSchemaName, BaseCatalogName, BaseTableName, and BaseColumnName.
To get the column metadata
1. Use the following code to retrieve the rows and the metadata by name. The BaseSchemaName, BaseCatalogName, and BaseTableName are always present.
public void TestReaderMetadata()
{
CompositeConnection conn = GetConnection();
Try
{
CompositeCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT * FROM PRODUCTS WHERE ProductID=1111";
CompositeDataReader reader = cmd.ExecuteReader();
DataTable dt = reader.GetSchemaTable();
INT ROWS = dt.Rows.Count;
IF (rows > 0)
{
foreach (DataRow row in dt.Rows)
{
String ColumnName = (String)row["ColumnName"];
int ColumnOrdinal = (int)row["ColumnOrdinal"];
int ColumnSize = (int)row["ColumnSize"];
int NumericPrecision = (int)row["NumericPrecision"];
int NumericScale = (int)row["NumericScale"];
Type DataType = (Type)row["DataType"];
int ProviderType = (int)row["ProviderType"];
bool IsLong = (bool)row["IsLong"];
bool AllowDBNull = (bool)row["AllowDBNull"];
bool IsReadOnly = (bool)row["IsReadOnly"];
bool IsRowVersion = (bool)row["IsRowVersion"];
bool IsUnique = (bool)row["IsUnique"];
bool IsKey = (bool)row["IsKey"];
bool IsAutoIncrement = (bool)row["IsAutoIncrement"];
String BaseSchemaName = (String)row["BaseSchemaName"];
String BaseCatalogName = (String)row["BaseCatalogName"];
String BaseTableName = (String)row["BaseTableName"];
String BaseColumnName = (String)row["BaseColumnName"];
Console.WriteLine("Column properties:");
Console.WriteLine("ColumnName:" + ColumnName+
",ColumnOrdinal:" + ColumnOrdinal+
",ColumnSize:" + ColumnSize+
",NumericPrecision:" + NumericPrecision+
",NumericScale:" + NumericScale+
",DataType:" + DataType+
",ProviderType:" + ProviderType+
",IsLong:" + IsLong+
",AllowDBNull:" + AllowDBNull+
",IsReadOnly:" + IsReadOnly+
",IsRowVersion:" + IsRowVersion+
",IsUnique:" + IsUnique+
",IsKey:" + IsKey+
",IsAutoIncrement:" + IsAutoIncrement+
",BaseSchemaName:" + BaseSchemaName+
",BaseCatalogName:" + BaseCatalogName+
",BaseTableName:" + BaseTableName+
",BaseColumnName:" + BaseColumnName+"."
);
}
}
reader.Close();
}
catch (Exception ex)
{
Assert.Fail(ex.Message);
}
}