473,729 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GetSchema to retrieve column properties

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("O rders") 'Orders is a table in the
datasource
For Each col In tbl.Columns
x += 1
colName(x) = col.ColumnName. ToString()
colType(x) = col.DataType.To String()
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
5 25570
What is your data source? Is it SQLServer?

Robin S.
-----------------------
"Ken" <fk****@yahoo.c omwrote in message
news:11******** **************@ t38g2000prd.goo glegroups.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("O rders") 'Orders is a table in the
datasource
For Each col In tbl.Columns
x += 1
colName(x) = col.ColumnName. ToString()
colType(x) = col.DataType.To String()
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
Yes.

Apr 26 '07 #3

"Randy" <ra************ @gmail.comwrote in message
news:11******** **************@ r30g2000prh.goo glegroups.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 = NumericPrecisio n, 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 = ProviderSpecifi cDataType, type = System.Type
col name = DataTypeName, type = System.String
col name = XmlSchemaCollec tionDatabase, type = System.String
col name = XmlSchemaCollec tionOwningSchem a, type = System.String
col name = XmlSchemaCollec tionName, type = System.String
col name = UdtAssemblyQual ifiedName, type = System.String
col name = NonVersionedPro viderType, 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(M y.Settings.DBCo nnString)
'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(SQLS tring, cn)
Dim rdr As SqlDataReader =
cmd.ExecuteRead er(CommandBehav ior.KeyInfo)
Dim tbl As DataTable = rdr.GetSchemaTa ble
'This shows all of the information you can access about each
column.
For Each col As DataColumn In tbl.Columns
Debug.Print("co l name = " & col.ColumnName & _
", type = " & col.DataType.To String)
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("DataTypeNa me"), row("IsExpressi on"))
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
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.connectionst ring = My.Settings.Acc DataConnectionS tring

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.ExecuteRead er(CommandBehav ior.KeyInfo)

'Retrieve column schema into a DataTable.

schemaTable = myReader.GetSch emaTable()

''For each field in the table...

For Each myField In schemaTable.Row s

'For each property of the field...

For Each myProperty In schemaTable.Col umns

' 'Display the field name and value.

Console.WriteLi ne(myProperty.C olumnName & " = " & myField(myPrope rty).ToString() )

Next

Console.WriteLi ne()

' 'Pause.

Console.ReadLin e()

Next

Best Regards,

Luqman

"Ken" <fk****@yahoo.c omwrote in message news:11******** **************@ t38g2000prd.goo glegroups.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("O rders") 'Orders is a table in the
datasource
For Each col In tbl.Columns
x += 1
colName(x) = col.ColumnName. ToString()
colType(x) = col.DataType.To String()
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
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.FillSch ema (schema, SchemaType.Sour ce)

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

using ( SqlConnection connection = new SqlConnection (
connectionStrin g ) )
{
using ( SqlCommand command = new SqlCommand ( statement,
connection ) )
{
using ( SqlDataAdapter adapter = new SqlDataAdapter (
command ) )
{
DataTable schema = new DataTable ();
adapter.FillSch ema ( schema, SchemaType.Sour ce );
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.ColumnNa me, column.DataType , column.AllowDBN ull, column.Ordinal ) );
}
}
}
}

fieldList.Sort ( FieldInfoCompar er.Instance );
return fieldList;
}

I do this for Stored Procs:

private static List<FieldInfoG etProcedureFiel dList ( string
procedure, string connectionStrin g )
{
List<FieldInfof ieldList = new List<FieldInfo( );
using ( SqlConnection connection = new SqlConnection (
connectionStrin g ) )
{
using ( SqlCommand command = new SqlCommand ( procedure,
connection ) )
{
command.Command Type = CommandType.Sto redProcedure;
using ( SqlDataAdapter adapter = new SqlDataAdapter (
command ) )
{
command.Connect ion.Open ();
SqlCommandBuild er.DeriveParame ters ( command );
DataTable schema = new DataTable ();
adapter.FillSch ema ( schema, SchemaType.Sour ce );
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.ColumnNa me, column.DataType , column.AllowDBN ull, column.Ordinal ) );
}
}
}
}

fieldList.Sort ( FieldInfoCompar er.Instance );
return fieldList;
}

--
Tom Shelton

"Luqman" <pe*******@cybe r.net.pkwrote in message
news:ep******** ******@TK2MSFTN GP03.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.connectionst ring = My.Settings.Acc DataConnectionS tring
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.ExecuteRead er(CommandBehav ior.KeyInfo)
'Retrieve column schema into a DataTable.
schemaTable = myReader.GetSch emaTable()
''For each field in the table...
For Each myField In schemaTable.Row s
'For each property of the field...
For Each myProperty In schemaTable.Col umns
' 'Display the field name and value.
Console.WriteLi ne(myProperty.C olumnName & " = " &
myField(myPrope rty).ToString() )
Next
Console.WriteLi ne()
' 'Pause.
Console.ReadLin e()
Next
Best Regards,
Luqman
"Ken" <fk****@yahoo.c omwrote in message
news:11******** **************@ t38g2000prd.goo glegroups.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("O rders") 'Orders is a table in the
datasource
For Each col In tbl.Columns
x += 1
colName(x) = col.ColumnName. ToString()
colType(x) = col.DataType.To String()
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2002
by: Dimitris Katsikas | last post by:
Hi everybody, I am trying to find a way to export automatically an entire database schema into an .xsd A graphical way is to drag & drop the tables from Server Connection into an empty .xsd file. My problem is that the xsd does not include info concerning the relationships between database tables. Is there an automated way to retrieve programmatically the entire db schema into a single xsd file?
1
3835
by: Leepe | last post by:
example: I have a skill object that implements the XmlSerializable interface Implementing the getschema gives me some unwanted generations towards the wsdl.exe Reference.cs generation. skill is an object with properties name and code public System.Xml.Schema.XmlSchema GetSchema() { string _namespace;
1
5061
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported with the ADSI NT Provider and only supported in the LDAP Provider. So given an UserId (UID) how can I read the First Name and Last Name using LDAP Provider. If anybody can help me with a C# sample code it would of great help. Thanks in advance.
1
6197
by: nathan.gomez | last post by:
Thanks for your time. I've been trying to get a list of databases available on a mySQL server (local) using the OdbcConnection.GetSchema() function. Thus far I have only been able to get "views". Can anyone help me out? code: OdbcConnection c = new OdbcConnection(); c.ConncetionString = conString; // the connection works fine, so I have not bothered to add it
0
5636
by: Hexman | last post by:
Hello All, I am wanting to make a small utility that allows the user (me) to select a local database (currently Access 2000), build a list of non-system tables, then select a table which will then populate a datagridview with the column name, default, null, if primary key, etc...... I've seen the code for obtaining this info before, but can't rememeber where. Can anyone help? Using vb.net 2005. The code below allows the user to...
1
1922
by: Mel | last post by:
Anyone know how I would retrieve the MaxLength property of a column in my Access Database table? I know how to retrieve table data, for example the "Quote #" field in my example code below, but I have never attempted to access a property of a field. 'Beginning of my Code Example Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\support\webbmq.mdb;" Dim strSel As String = "SELECT * FROM " Dim conWebOrdNum As New...
0
1181
by: NETCODE | last post by:
I am developing an add-in. I need to retrieve the path of .msi file, located in under project properties, from the deployment project (setup). I was able retieve all project properties of all projects (ex: output path of the dll) except Deployment project. I am using "project.ConfigurationManager.ActiveConfiguration.Properties" and "Project.Properties" for retrieving all information. But I am stil could not able to retrieve the path...
0
2720
by: Sebastijan | last post by:
Hi I am interested in DbConnection.GetSchema method of ODP.NET oracle library. When I call (to get primary keys metadata) DataTable pkeys = DbConnection.GetSchema("PrimaryKeys"... foreach (DataRow dr in pkeys.Rows) { string pkTable = dr.ToString();
3
3222
by: Jaydeep Ramavat | last post by:
Dear Friends, how can we get the default value of the columns using getschema functionality. is there any alternative is available? Regards Jay.
0
8917
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.