By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,612 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,612 IT Pros & Developers. It's quick & easy.

GetSchema to retrieve column properties

P: n/a
Ken
I'm trying to run a loop to capture column property information from a
table in my datasource. Can anybody see where this is going wrong?

Dim tbl As New DataTable
Dim col As DataColumn
Dim x As Integer
Dim colName(99) As String
Dim colType(99) As String
cn.Open()
tbl = cn.GetSchema("Orders") 'Orders is a table in the
datasource
For Each col In tbl.Columns
x += 1
colName(x) = col.ColumnName.ToString()
colType(x) = col.DataType.ToString()
Next
cn.Close()

If it's not obvious, my intended result are two variables for each
column from the original datatable that will capture a) the name of
the column, and b) the datatype of the column. I will then use these
variables to drive subsequent logic.

I'm getting an error on the GetSchema line: The requested collection
(Orders) is not defined.

I have little experience with the GetSchema method, so any advice on
how to accomplish this is appreciated.

Thx,
Ken

Apr 25 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
What is your data source? Is it SQLServer?

Robin S.
-----------------------
"Ken" <fk****@yahoo.comwrote in message
news:11**********************@t38g2000prd.googlegr oups.com...
I'm trying to run a loop to capture column property information from a
table in my datasource. Can anybody see where this is going wrong?

Dim tbl As New DataTable
Dim col As DataColumn
Dim x As Integer
Dim colName(99) As String
Dim colType(99) As String
cn.Open()
tbl = cn.GetSchema("Orders") 'Orders is a table in the
datasource
For Each col In tbl.Columns
x += 1
colName(x) = col.ColumnName.ToString()
colType(x) = col.DataType.ToString()
Next
cn.Close()

If it's not obvious, my intended result are two variables for each
column from the original datatable that will capture a) the name of
the column, and b) the datatype of the column. I will then use these
variables to drive subsequent logic.

I'm getting an error on the GetSchema line: The requested collection
(Orders) is not defined.

I have little experience with the GetSchema method, so any advice on
how to accomplish this is appreciated.

Thx,
Ken

Apr 25 '07 #2

P: n/a
Yes.

Apr 26 '07 #3

P: n/a

"Randy" <ra************@gmail.comwrote in message
news:11**********************@r30g2000prh.googlegr oups.com...
Yes.
Try this:
Here are all of the properties (and their types) you can retrieve for the
data columns using a DataReader.

col name = ColumnName, type = System.String
col name = ColumnOrdinal, type = System.Int32
col name = ColumnSize, type = System.Int32
col name = NumericPrecision, type = System.Int16
col name = NumericScale, type = System.Int16
col name = IsUnique, type = System.Boolean
col name = IsKey, type = System.Boolean
col name = BaseServerName, type = System.String
col name = BaseCatalogName, type = System.String
col name = BaseColumnName, type = System.String
col name = BaseSchemaName, type = System.String
col name = BaseTableName, type = System.String
col name = DataType, type = System.Type
col name = AllowDBNull, type = System.Boolean
col name = ProviderType, type = System.Int32
col name = IsAliased, type = System.Boolean
col name = IsExpression, type = System.Boolean
col name = IsIdentity, type = System.Boolean
col name = IsAutoIncrement, type = System.Boolean
col name = IsRowVersion, type = System.Boolean
col name = IsHidden, type = System.Boolean
col name = IsLong, type = System.Boolean
col name = IsReadOnly, type = System.Boolean
col name = ProviderSpecificDataType, type = System.Type
col name = DataTypeName, type = System.String
col name = XmlSchemaCollectionDatabase, type = System.String
col name = XmlSchemaCollectionOwningSchema, type = System.String
col name = XmlSchemaCollectionName, type = System.String
col name = UdtAssemblyQualifiedName, type = System.String
col name = NonVersionedProviderType, type = System.Int32

Here's how I got this list; tableName is passed in as a String. This shows
the columns you can get, and then shows selected values for each column
defined in the table.
Dim cn As New SqlConnection(My.Settings.DBConnString)
'put the table name in brackets in case it has spaces in it
Dim SQLString As String = "SELECT * FROM [" & tableName & "]"
Try
cn.Open()
Dim cmd As New SqlCommand(SQLString, cn)
Dim rdr As SqlDataReader =
cmd.ExecuteReader(CommandBehavior.KeyInfo)
Dim tbl As DataTable = rdr.GetSchemaTable
'This shows all of the information you can access about each
column.
For Each col As DataColumn In tbl.Columns
Debug.Print("col name = " & col.ColumnName & _
", type = " & col.DataType.ToString)
Next
For Each row As DataRow In tbl.Rows
'DataTypeName actually gives the same
' data type name as is displayed in SQLServer
Debug.Print("{0}, ColumnSize = {1}, DataType = {2},
DataTypeName = {3}, IsExpression = {4} ", _
row("ColumnName"), row("ColumnSize"), row("DataType"), _
row("DataTypeName"), row("IsExpression"))
Next
rdr.Close()
Catch
MessageBox.Show("Error opening the connection to the database.")
Finally
cn.Close()
End Try

I also know how to get a list of tables if you're interested.

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
Apr 27 '07 #4

P: n/a
Hi,

Try following:-

'Retrieve Dataset Schema

Dim cn As New SqlConnection()

Dim cmd As New SqlCommand()

Dim schemaTable As DataTable

Dim myReader As SqlDataReader

'Dim myField As DataRow

'Dim myProperty As DataColumn

cn.connectionstring = My.Settings.AccDataConnectionString

cn.Open()

'Retrieve records from the Employees table into a DataReader.

cmd.Connection = cn

''I used 1=0 because I don't want to waste time in retrieving the records, just a Schema

cmd.CommandText = "SELECT * FROM Customers Where 1=0"

myReader = cmd.ExecuteReader(CommandBehavior.KeyInfo)

'Retrieve column schema into a DataTable.

schemaTable = myReader.GetSchemaTable()

''For each field in the table...

For Each myField In schemaTable.Rows

'For each property of the field...

For Each myProperty In schemaTable.Columns

' 'Display the field name and value.

Console.WriteLine(myProperty.ColumnName & " = " & myField(myProperty).ToString())

Next

Console.WriteLine()

' 'Pause.

Console.ReadLine()

Next

Best Regards,

Luqman

"Ken" <fk****@yahoo.comwrote in message news:11**********************@t38g2000prd.googlegr oups.com...
I'm trying to run a loop to capture column property information from a
table in my datasource. Can anybody see where this is going wrong?

Dim tbl As New DataTable
Dim col As DataColumn
Dim x As Integer
Dim colName(99) As String
Dim colType(99) As String
cn.Open()
tbl = cn.GetSchema("Orders") 'Orders is a table in the
datasource
For Each col In tbl.Columns
x += 1
colName(x) = col.ColumnName.ToString()
colType(x) = col.DataType.ToString()
Next
cn.Close()

If it's not obvious, my intended result are two variables for each
column from the original datatable that will capture a) the name of
the column, and b) the datatype of the column. I will then use these
variables to drive subsequent logic.

I'm getting an error on the GetSchema line: The requested collection
(Orders) is not defined.

I have little experience with the GetSchema method, so any advice on
how to accomplish this is appreciated.

Thx,
Ken
May 16 '07 #5

P: n/a
Or, as an alternative you can use the SqlDataAdapter.FillSchema method like
so:

cmd.CommandText = "SELECT * FROM Customers"
Dim schema As New DataTable
Dim adapter As New SqlDataAdapter(cmd)
adapter.FillSchema (schema, SchemaType.Source)

In fact, this is the method I use in a small code generation utility I wrote
(Warning C# code!):
private static List<FieldInfoGetTableFieldList ( string table,
string connectionString )
{
List<FieldInfofieldList = new List<FieldInfo();
string statement = string.Format ( "SELECT * FROM {0}", table );

using ( SqlConnection connection = new SqlConnection (
connectionString ) )
{
using ( SqlCommand command = new SqlCommand ( statement,
connection ) )
{
using ( SqlDataAdapter adapter = new SqlDataAdapter (
command ) )
{
DataTable schema = new DataTable ();
adapter.FillSchema ( schema, SchemaType.Source );
foreach ( DataColumn column in schema.Columns )
{
// TODO: Refactor FieldInfo to accept a
DataColumn as a parameter in it's constructor
fieldList.Add ( new FieldInfo ( table,
column.ColumnName, column.DataType, column.AllowDBNull, column.Ordinal ) );
}
}
}
}

fieldList.Sort ( FieldInfoComparer.Instance );
return fieldList;
}

I do this for Stored Procs:

private static List<FieldInfoGetProcedureFieldList ( string
procedure, string connectionString )
{
List<FieldInfofieldList = new List<FieldInfo();
using ( SqlConnection connection = new SqlConnection (
connectionString ) )
{
using ( SqlCommand command = new SqlCommand ( procedure,
connection ) )
{
command.CommandType = CommandType.StoredProcedure;
using ( SqlDataAdapter adapter = new SqlDataAdapter (
command ) )
{
command.Connection.Open ();
SqlCommandBuilder.DeriveParameters ( command );
DataTable schema = new DataTable ();
adapter.FillSchema ( schema, SchemaType.Source );
foreach ( DataColumn column in schema.Columns )
{
// TODO: Refactor FieldInfo to accept a
DataColumn as a parameter in it's constructor
fieldList.Add ( new FieldInfo ( procedure,
column.ColumnName, column.DataType, column.AllowDBNull, column.Ordinal ) );
}
}
}
}

fieldList.Sort ( FieldInfoComparer.Instance );
return fieldList;
}

--
Tom Shelton

"Luqman" <pe*******@cyber.net.pkwrote in message
news:ep**************@TK2MSFTNGP03.phx.gbl...
Hi,

Try following:-

'Retrieve Dataset Schema
Dim cn As New SqlConnection()
Dim cmd As New SqlCommand()
Dim schemaTable As DataTable
Dim myReader As SqlDataReader
'Dim myField As DataRow
'Dim myProperty As DataColumn
cn.connectionstring = My.Settings.AccDataConnectionString
cn.Open()
'Retrieve records from the Employees table into a DataReader.
cmd.Connection = cn
''I used 1=0 because I don't want to waste time in retrieving the records,
just a Schema
cmd.CommandText = "SELECT * FROM Customers Where 1=0"
myReader = cmd.ExecuteReader(CommandBehavior.KeyInfo)
'Retrieve column schema into a DataTable.
schemaTable = myReader.GetSchemaTable()
''For each field in the table...
For Each myField In schemaTable.Rows
'For each property of the field...
For Each myProperty In schemaTable.Columns
' 'Display the field name and value.
Console.WriteLine(myProperty.ColumnName & " = " &
myField(myProperty).ToString())
Next
Console.WriteLine()
' 'Pause.
Console.ReadLine()
Next
Best Regards,
Luqman
"Ken" <fk****@yahoo.comwrote in message
news:11**********************@t38g2000prd.googlegr oups.com...
I'm trying to run a loop to capture column property information from a
table in my datasource. Can anybody see where this is going wrong?

Dim tbl As New DataTable
Dim col As DataColumn
Dim x As Integer
Dim colName(99) As String
Dim colType(99) As String
cn.Open()
tbl = cn.GetSchema("Orders") 'Orders is a table in the
datasource
For Each col In tbl.Columns
x += 1
colName(x) = col.ColumnName.ToString()
colType(x) = col.DataType.ToString()
Next
cn.Close()

If it's not obvious, my intended result are two variables for each
column from the original datatable that will capture a) the name of
the column, and b) the datatype of the column. I will then use these
variables to drive subsequent logic.

I'm getting an error on the GetSchema line: The requested collection
(Orders) is not defined.

I have little experience with the GetSchema method, so any advice on
how to accomplish this is appreciated.

Thx,
Ken
May 17 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.