May 04, 2004

Retrieving Column Schema

There are several occasions where we need data table schema like column name, size, data type etc etc. Here is the code which performs same by extracting information from table schema.

On this msdn page you can get additional list of schema columns. You can also see all columns and associated schema by assigning schemaTable to DataGrid.

private void CheckSchemaTable() {
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = "Server=myserver;Database=asm_db1;Integrated Security=true;";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from myTable";
cmd.Connection = cnn;
SqlDataReader myReader = cmd.ExecuteReader();
DataTable schemaTable = myReader.GetSchemaTable();
// For each field in the table...
for (int i = 0; i < myfield =" schemaTable.Rows[i];" colname =" myField[" colsize =" myField[" coldatatype =" myField[" colnull =" myField[">