How to get meta data information of table

Posted by Goldytech under Others category on | Views : 2982
Suppose you are required to get the table structure information like what are its columns names and what are their data types whether it accepts null values etc
Here is it how you can extract that info programatically with

Sub GetTableMetaData()
Dim con As New SqlConnection("your db connection string")
Dim cmd As New SqlCommand("Select * from yourTblName")
cmd.Connection = con
Dim rdr As SqlDataReader
rdr = cmd.ExecuteReader
Dim tbl As DataTable
tbl = rdr.GetSchemaTable
For Each row As DataRow In tbl.Rows
Console.WriteLine("{0}: {1} :{2}", row("ColumnName"), row("DataType"), row("AllowDBnull"))
End Sub

Comments or Responses

Login to post response