PC Review


Reply
Thread Tools Rate Thread

accessing column types without doing a query

 
 
headware
Guest
Posts: n/a
 
      25th Jul 2003
Is there a convinient way to access column types without having to
actually perform a query? I know about the GetOleDbSchemaTable()
function but I don't see anything about column types there.

Thanks,
Dave
 
Reply With Quote
 
 
 
 
Joe Fallon
Guest
Posts: n/a
 
      26th Jul 2003
David Sceppa's book ADO.Net contains a sample app for returning all of this
data in a convenient layout.
Includes the code needed to build the tables in your client app.

I thought it used GetOleDbSchemaTable but I could be wrong.
--
Joe Fallon



"headware" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Is there a convinient way to access column types without having to
> actually perform a query? I know about the GetOleDbSchemaTable()
> function but I don't see anything about column types there.
>
> Thanks,
> Dave



 
Reply With Quote
 
Paul Clement
Guest
Posts: n/a
 
      28th Jul 2003
On 25 Jul 2003 11:21:36 -0700, (E-Mail Removed) (headware) wrote:

¤ Is there a convinient way to access column types without having to
¤ actually perform a query? I know about the GetOleDbSchemaTable()
¤ function but I don't see anything about column types there.
¤

Yes, GetOleDbSchemaTable will do this:

Sub ListTableSchema()

Dim AccessConnection As New System.Data.OleDb.OleDbConnection()
Dim SchemaTable As DataTable

AccessConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=E:\My Documents\db1.mdb"
AccessConnection.Open()

'Retrieve schema information about Table1.
SchemaTable =
AccessConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Columns, _
New Object() {Nothing, Nothing, "Table1"})

DataGrid1.DataSource = SchemaTable

AccessConnection.Close()

End Sub

You will need to match up DATA_TYPE with values in the System.Data.OleDb.OleDbType enum.


Paul ~~~ (E-Mail Removed)
Microsoft MVP (Visual Basic)
 
Reply With Quote
 
David Sceppa
Guest
Posts: n/a
 
      28th Jul 2003

Each DataReader object exposes a GetSchemaTable method that
returns a DataTable of schema information about the resultset.
Each row in the schema table corresponds to a column in the
resultset. You can use this schema information to get column
names, data types, etc. The .NET data type (System.String) is
available in the DataType column and the .NET provider-specific
data type (OleDbType.VarWChar) is available in the ProviderType
column, though the data is returned as simply an integer.

You can call Command.ExecuteQuery and ask for schema
information without actually executing the query by supplying a
CommandBehavior of SchemaOnly.

Supplying a CommandBehavior of KeyInfo will include
additional schema information such as base column and table
names, key column information, etc.

I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2003 Microsoft Corporation. All rights reserved.

 
Reply With Quote
 
headware
Guest
Posts: n/a
 
      29th Jul 2003
(E-Mail Removed) (David Sceppa) wrote in message news:<(E-Mail Removed)>...
> Each DataReader object exposes a GetSchemaTable method that
> returns a DataTable of schema information about the resultset.
> Each row in the schema table corresponds to a column in the
> resultset. You can use this schema information to get column
> names, data types, etc. The .NET data type (System.String) is
> available in the DataType column and the .NET provider-specific
> data type (OleDbType.VarWChar) is available in the ProviderType
> column, though the data is returned as simply an integer.
>
> You can call Command.ExecuteQuery and ask for schema
> information without actually executing the query by supplying a
> CommandBehavior of SchemaOnly.
>
> Supplying a CommandBehavior of KeyInfo will include
> additional schema information such as base column and table
> names, key column information, etc.
>
> I hope this information proves helpful.
>
> David Sceppa
> Microsoft
> This posting is provided "AS IS" with no warranties,
> and confers no rights. You assume all risk for your use.
> © 2003 Microsoft Corporation. All rights reserved.


The only problem I found with using the
OleDbDataReader.GetSchemaTable() method is that you can't find the
type for a specific field by name like this:

DataTable schemTbl = dataReader.GetSchemaTable();
DataRow row = schemTbl.Rows["age"]; //can't do this
Console.WriteLine(row["DataType"]);

you have to use the integer index of the row since you can't index the
Rows collection with a string, so you end up having to do this:

DataTable schemTbl = dataReader.GetSchemaTable();
DataRow row = schemTbl.Rows[0]; //can't use the column name here
Console.WriteLine(row["DataType"]);

and I really would like to be able to get the field type based on the
field name and not on its index in the table. Fortunately, while
looking into your suggestion, I found that I can get the schema info I
want from the OleDbDataReader object itself by using the
GetFieldType() and GetOrdinal() methods like this:

Console.WriteLine(dr.GetFieldType(dr.GetOrdinal("age")));

It's too bad that they decided to provide so many ways of getting
schema info (through GetSchemaTable(), GetOleDbSchemaTable(), and the
GetFieldType() methods to name a few) because it gets a little
confusing. Let me know if I'm wrong about not being able to use the
field name with the GetSchemaTable() function.

Dave
 
Reply With Quote
 
David Sceppa
Guest
Posts: n/a
 
      29th Jul 2003
Dave,

> The only problem I found with using the
> OleDbDataReader.GetSchemaTable() method is that you can't
> find the type for a specific field by name like this:
>
> DataTable schemTbl = dataReader.GetSchemaTable();
> DataRow row = schemTbl.Rows["age"]; //can't do this
> Console.WriteLine(row["DataType"]);


Actually, with a slight change to the code, there are a
couple different ways you can do this.

You can locate a row in the schema table based on the
resultset's column name by setting the schema table's primary key:

schemTbl.PrimaryKey = new DataColumn[]
{schemTbl.Columns["ColumnName"]};
DataRow row = schemTbl.Rows.Find(strColumnName);
Console.WriteLine(row["DataType"]);


Or you could ask the DataReader for a column's ordinal in
the resultset, and use that information to locate the row in the
schema table:

int intColumnIndex = dataReader.GetOrdinal(strColumnName);
DataRow row = schemTbl.Rows[intColumnIndex];
Console.WriteLine(row["DataType"]);


David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2003 Microsoft Corporation. All rights reserved.

 
Reply With Quote
 
headware
Guest
Posts: n/a
 
      29th Jul 2003
Both good suggestions. Thanks for the help.

Dave

(E-Mail Removed) (David Sceppa) wrote in message news:<(E-Mail Removed)>...
> Dave,
>
> > The only problem I found with using the
> > OleDbDataReader.GetSchemaTable() method is that you can't
> > find the type for a specific field by name like this:
> >
> > DataTable schemTbl = dataReader.GetSchemaTable();
> > DataRow row = schemTbl.Rows["age"]; //can't do this
> > Console.WriteLine(row["DataType"]);

>
> Actually, with a slight change to the code, there are a
> couple different ways you can do this.
>
> You can locate a row in the schema table based on the
> resultset's column name by setting the schema table's primary key:
>
> schemTbl.PrimaryKey = new DataColumn[]
> {schemTbl.Columns["ColumnName"]};
> DataRow row = schemTbl.Rows.Find(strColumnName);
> Console.WriteLine(row["DataType"]);
>
>
> Or you could ask the DataReader for a column's ordinal in
> the resultset, and use that information to locate the row in the
> schema table:
>
> int intColumnIndex = dataReader.GetOrdinal(strColumnName);
> DataRow row = schemTbl.Rows[intColumnIndex];
> Console.WriteLine(row["DataType"]);
>
>
> David Sceppa
> Microsoft
> This posting is provided "AS IS" with no warranties,
> and confers no rights. You assume all risk for your use.
> © 2003 Microsoft Corporation. All rights reserved.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically Loading Assembly and Accessing its Types (namespaces are different) Smithers Microsoft C# .NET 7 5th Aug 2007 03:07 PM
Needed: Chart that combines clustered column and stacked column types Gerry Microsoft Excel Charting 3 14th Feb 2007 02:53 AM
Accessing COM (VB6) User-Defined types (UDTs) from a VB.NET dll jimfollett1 via DotNetMonster.com Microsoft VB .NET 3 19th Oct 2005 07:13 PM
Datarelation between different column types Jason James Microsoft ADO .NET 5 5th Aug 2005 07:41 AM
How do I mix column chart types? =?Utf-8?B?UmFtZXNoIE5hcmFzaW1oYW4=?= Microsoft Excel Charting 1 20th Jul 2005 11:23 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:15 PM.