DataRow.Item return

  • Thread starter Thread starter Edward Diener
  • Start date Start date
E

Edward Diener

The return value for a DataRow.Item[string] is a System.Object. How can I
know beforehand, for a given RDBMS and that RDBMS's column type for the
column specified by the string, what specific .NET object that System.Object
actually is ?
 
I am looking for some documentation that tells me beforehand, for a given
RDBMS, what the .NET System.Object is for a given column type in that RDBMS.
Does such documentation exist ?

Rich said:
DataRow[x].GetType.ToString

where "x" is the column ordinal position

Edward Diener said:
The return value for a DataRow.Item[string] is a System.Object. How can I
know beforehand, for a given RDBMS and that RDBMS's column type for the
column specified by the string, what specific .NET object that
System.Object
actually is ?
 
Edward Diener said:
I am looking for some documentation that tells me beforehand, for a given
RDBMS, what the .NET System.Object is for a given column type in that
RDBMS. Does such documentation exist ?

If such documentation existed, it could still change in the future. You'd be
better off writing your application to not need to know at compile-time.

John Saunders

Rich said:
DataRow[x].GetType.ToString

where "x" is the column ordinal position

Edward Diener said:
The return value for a DataRow.Item[string] is a System.Object. How can
I
know beforehand, for a given RDBMS and that RDBMS's column type for the
column specified by the string, what specific .NET object that
System.Object
actually is ?
 
John Saunders said:
If such documentation existed, it could still change in the future. You'd
be better off writing your application to not need to know at
compile-time.

And how would I do this ? Surely I need to know what .NET type to use in
order to read in a column value.

I have asked this question numerous times here on this NG and am either
missing something or not communicating in a way that others understand. I
will try again: I have a column in a database table and need to read the
value of that column and manipulate it in a .NET variable. How do I know
what type of .NET variable to use ? Does this make more sense ? If not I do
not know how to ask this in a way others will understand.
John Saunders

Rich said:
DataRow[x].GetType.ToString

where "x" is the column ordinal position

:

The return value for a DataRow.Item[string] is a System.Object. How can
I
know beforehand, for a given RDBMS and that RDBMS's column type for the
column specified by the string, what specific .NET object that
System.Object
actually is ?
 
Perhaps (for SQL Server) :
http://msdn.microsoft.com/library/d...f/html/frlrfsystemdatasqldbtypeclasstopic.asp

Patrice

--

Edward Diener said:
John Saunders said:
If such documentation existed, it could still change in the future. You'd
be better off writing your application to not need to know at
compile-time.

And how would I do this ? Surely I need to know what .NET type to use in
order to read in a column value.

I have asked this question numerous times here on this NG and am either
missing something or not communicating in a way that others understand. I
will try again: I have a column in a database table and need to read the
value of that column and manipulate it in a .NET variable. How do I know
what type of .NET variable to use ? Does this make more sense ? If not I do
not know how to ask this in a way others will understand.
John Saunders

DataRow[x].GetType.ToString

where "x" is the column ordinal position

:

The return value for a DataRow.Item[string] is a System.Object. How can
I
know beforehand, for a given RDBMS and that RDBMS's column type for the
column specified by the string, what specific .NET object that
System.Object
actually is ?
 
Edward Diener said:
And how would I do this ? Surely I need to know what .NET type to use in
order to read in a column value.

I have asked this question numerous times here on this NG and am either
missing something or not communicating in a way that others understand. I
will try again: I have a column in a database table and need to read the
value of that column and manipulate it in a .NET variable. How do I know
what type of .NET variable to use ? Does this make more sense ? If not I
do not know how to ask this in a way others will understand.

What kind of manipulation do you want to do? If you don't know what type it
is at all, if it could be any type at all, then you're quite limited. In
fact, you're limited to the methods of the System.Object type.

You could go a bit further, and look at the type as returned by
Object.GetType(). There's a world of information in the Type class!

Further, you can check to see if it implements IConvertible, in which case
you can try to convert it to various other types.

You can examine it in more detail with the members of the TypeDescriptor
class. In particular, TypeDescriptor.GetConverter.

And finally, you can go more deeply into the type by using Reflection to
find out everything there is to know about the type.


In writing your program, you should ask yourself: if you knew the .NET type
ahead of time, what would you do differently? If it were an int, for
instance.

Ok, now imagine that the .NET type changes a little. Maybe to a double. How
would you rewrite the code?

Next, imagine that it changes a bit more radically, like to a string. What
changes?

If you continue with this thought experiment, you may be able to focus on
what part of your program is dependant on the data type and what part is
not.

I hope that's helpful.

John Saunders
 
John Saunders said:
What kind of manipulation do you want to do? If you don't know what type
it is at all, if it could be any type at all, then you're quite limited.
In fact, you're limited to the methods of the System.Object type.

The idea is to know what type it is so that the value for a column can be
put in that type and manipulated internally that way. Otherwise, as you say,
one is stuck with System.Object. What is wrong with knowing this in advance
? For instance, a varchar2 column type is usually, I would guess, a
System.String. Some RDBMSs might have column types where one wonders what
actual .NET data type I can use internally. This seems for me to be normal,
to know in advance, for a particular RDBMS and for a particular column type
in that RDBMS, what .NET data type corresponds. I am still baffled that, for
a given RDBMS, I can not get information on how to find this out in some
sort of documentation.

Without this predetermined information one is stuck with writing code which
treats all column data purely as System.Object.
You could go a bit further, and look at the type as returned by
Object.GetType(). There's a world of information in the Type class!

OK, that is fair. But I felt that documentation would tell me this rather
than for me having to set up a table with all the column data types for a
particular RDBMS, and then examine Object.GetType() to find out what .NET
type exists for each column type. But I can do the latter. Still you seem to
be saying that an RDBMS vendor need not publish this information for a
programmer and that it is up to programmers to discover this for themselves.
I find that an odd way at looking at documentation in this case.
Further, you can check to see if it implements IConvertible, in which case
you can try to convert it to various other types.

You can examine it in more detail with the members of the TypeDescriptor
class. In particular, TypeDescriptor.GetConverter.

And finally, you can go more deeply into the type by using Reflection to
find out everything there is to know about the type.


In writing your program, you should ask yourself: if you knew the .NET
type ahead of time, what would you do differently? If it were an int, for
instance.

I would put the value in a variable which was of type 'int' rather than type
System.Object and perhaps manipulate it as such.
Ok, now imagine that the .NET type changes a little. Maybe to a double.
How would you rewrite the code?

Put it in a 'double' rather than an 'int'. Obviously if the column now
supports floating point rather than just whole numbers, the application
probably changes.
Next, imagine that it changes a bit more radically, like to a string. What
changes?

If you continue with this thought experiment, you may be able to focus on
what part of your program is dependant on the data type and what part is
not.

It is not a matter of focus but just a matter of knowing. Have you ever
programmed applications where you had no idea at run-time what data type to
use for a particular value ? Somehow I doubt that, but maybe you have. When
I am dealing with a strongly typed language like C# or C++ I actually want
to know what data types I have. Keeping data with an amorphous
System.Object, and testing its type at run-time in order to manipulate it,
is not my idea of easy programming.
 
Thanks, that tells me the correspondence for SQL Server. Now all I have to
do is find the same one for Oracle, which is the database I am using with
ADO .NET.

Patrice said:
Perhaps (for SQL Server) :
http://msdn.microsoft.com/library/d...f/html/frlrfsystemdatasqldbtypeclasstopic.asp

Patrice

--

Edward Diener said:
John Saunders said:
I am looking for some documentation that tells me beforehand, for a given
RDBMS, what the .NET System.Object is for a given column type in that
RDBMS. Does such documentation exist ?

If such documentation existed, it could still change in the future. You'd
be better off writing your application to not need to know at
compile-time.

And how would I do this ? Surely I need to know what .NET type to use in
order to read in a column value.

I have asked this question numerous times here on this NG and am either
missing something or not communicating in a way that others understand. I
will try again: I have a column in a database table and need to read the
value of that column and manipulate it in a .NET variable. How do I know
what type of .NET variable to use ? Does this make more sense ? If not I do
not know how to ask this in a way others will understand.
John Saunders


DataRow[x].GetType.ToString

where "x" is the column ordinal position

:

The return value for a DataRow.Item[string] is a System.Object. How can
I
know beforehand, for a given RDBMS and that RDBMS's column type for the
column specified by the string, what specific .NET object that
System.Object
actually is ?
 
Just use GetType once as a dev tool to see what is the type returned. You
can define then the var and built your own chart over time.
For simple types it would be obvious (string, boolean). For numeric types it
could be a bit more problematic.

Also always enforce strong and strict typing (you can disable this if using
VB.NET) so that you'll be warn if the cast is not valid.

Patrice

--

Edward Diener said:
Thanks, that tells me the correspondence for SQL Server. Now all I have to
do is find the same one for Oracle, which is the database I am using with
ADO .NET.

Patrice said:
Perhaps (for SQL Server) :
http://msdn.microsoft.com/library/d...f/html/frlrfsystemdatasqldbtypeclasstopic.asp

Patrice

--

Edward Diener said:
"John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
I am looking for some documentation that tells me beforehand, for a given
RDBMS, what the .NET System.Object is for a given column type in that
RDBMS. Does such documentation exist ?

If such documentation existed, it could still change in the future. You'd
be better off writing your application to not need to know at
compile-time.

And how would I do this ? Surely I need to know what .NET type to use in
order to read in a column value.

I have asked this question numerous times here on this NG and am either
missing something or not communicating in a way that others understand. I
will try again: I have a column in a database table and need to read the
value of that column and manipulate it in a .NET variable. How do I know
what type of .NET variable to use ? Does this make more sense ? If not
I
do
not know how to ask this in a way others will understand.


John Saunders


DataRow[x].GetType.ToString

where "x" is the column ordinal position

:

The return value for a DataRow.Item[string] is a System.Object.
How
can
I
know beforehand, for a given RDBMS and that RDBMS's column type
for
the
column specified by the string, what specific .NET object that
System.Object
actually is ?
 
Edward Diener said:
The idea is to know what type it is so that the value for a column can be
put in that type and manipulated internally that way.

Edward, are you talking about one particular set of tables? You can create a
typed dataset, which will contain strongly-typed columns. For instance, if
you had a varchar2 column called "Name", the table would have a property
called "Name", of type "String".

I thought you were talking about doing this to generic data, and not to a
particular set of data from a particular set of queries....
It is not a matter of focus but just a matter of knowing. Have you ever
programmed applications where you had no idea at run-time what data type
to use for a particular value ? Somehow I doubt that, but maybe you have.
When I am dealing with a strongly typed language like C# or C++ I actually
want to know what data types I have. Keeping data with an amorphous
System.Object, and testing its type at run-time in order to manipulate it,
is not my idea of easy programming.

Again, I had assumed that you knew about typed datasets. Given this
assumption, if you were still asking, I figured you needed to access this
information generically. But it seems like typed datasets will give you
exactly what you want.

John Saunders
 
John Saunders said:
Edward, are you talking about one particular set of tables? You can create
a typed dataset, which will contain strongly-typed columns. For instance,
if you had a varchar2 column called "Name", the table would have a
property called "Name", of type "String".

I will look into typed datasets. Right now I am just getting a DataRow and
accessing a column value by myDataRow["SomeColumnName"]. I want to put the
result of that into a variable of the correct type other than System.Object.
If I know, that the column has some database type, let's say NUMBER, in some
RDBMS, let's say Oracle, I want to know beforehand what .NET type of
variable, let's say 'int', I should use to hold the value. That is really
all my original question involved. How does one find this out given a
particular RDBMS ? I am guessing there is documentation for this, but it is
probably somewhere in Oracle .NET documentation. As it turns out, since the
choice of provider has been dictated elsewhere, and it is the OleDb .NET
provider that is being used, I have to find out somewhere how Oracle column
types relate to the OleDbType, since the relationship between OleDbType and
..NET types are documented in MSDN. I have found an Oracle document on this
in a PDF file I was able to download from their site, so I am on my way.
Thanks for ytour help !
 
Patrice said:
Just use GetType once as a dev tool to see what is the type returned. You
can define then the var and built your own chart over time.
For simple types it would be obvious (string, boolean). For numeric types
it
could be a bit more problematic.

Yes, this has been suggested and, in lieu of documentation, is no doubt the
way to go. Luckily I found Oracle documentation for it so I now know the
correct mapping between Oracle column types and .NET types for the
particular provider I am using. Thanks !
Also always enforce strong and strict typing (you can disable this if
using
VB.NET) so that you'll be warn if the cast is not valid.

Patrice

--

Edward Diener said:
Thanks, that tells me the correspondence for SQL Server. Now all I have
to
do is find the same one for Oracle, which is the database I am using with
ADO .NET.

Patrice said:
Perhaps (for SQL Server) :
http://msdn.microsoft.com/library/d...f/html/frlrfsystemdatasqldbtypeclasstopic.asp

Patrice

--

"Edward Diener" <[email protected]> a écrit dans le message de

"John Saunders" <johnwsaundersiii at hotmail.com> wrote in message
I am looking for some documentation that tells me beforehand, for a
given
RDBMS, what the .NET System.Object is for a given column type in
that
RDBMS. Does such documentation exist ?

If such documentation existed, it could still change in the future.
You'd
be better off writing your application to not need to know at
compile-time.

And how would I do this ? Surely I need to know what .NET type to use in
order to read in a column value.

I have asked this question numerous times here on this NG and am
either
missing something or not communicating in a way that others
understand. I
will try again: I have a column in a database table and need to read the
value of that column and manipulate it in a .NET variable. How do I know
what type of .NET variable to use ? Does this make more sense ? If not I
do
not know how to ask this in a way others will understand.


John Saunders


DataRow[x].GetType.ToString

where "x" is the column ordinal position

:

The return value for a DataRow.Item[string] is a System.Object. How
can
I
know beforehand, for a given RDBMS and that RDBMS's column type for
the
column specified by the string, what specific .NET object that
System.Object
actually is ?
 
Edward Diener said:
John Saunders said:
Edward, are you talking about one particular set of tables? You can
create a typed dataset, which will contain strongly-typed columns. For
instance, if you had a varchar2 column called "Name", the table would
have a property called "Name", of type "String".

I will look into typed datasets. Right now I am just getting a DataRow and
accessing a column value by myDataRow["SomeColumnName"]. I want to put the
result of that into a variable of the correct type other than
System.Object. If I know, that the column has some database type, let's
say NUMBER, in some RDBMS, let's say Oracle, I want to know beforehand
what .NET type of variable, let's say 'int', I should use to hold the
value. That is really all my original question involved. How does one find
this out given a particular RDBMS ? I am guessing there is documentation
for this, but it is probably somewhere in Oracle .NET documentation. As it
turns out, since the choice of provider has been dictated elsewhere, and
it is the OleDb .NET provider that is being used, I have to find out
somewhere how Oracle column types relate to the OleDbType, since the
relationship between OleDbType and .NET types are documented in MSDN. I
have found an Oracle document on this in a PDF file I was able to download
from their site, so I am on my way.

Edward, just to be clear: if you use a typed dataset, you won't have to go
looking things up. All of the work will be done for you.

John Saunders
 
Back
Top