Database datatype to c# datatype

P

Peter Kirk

Hi

I am reading metadata from a database (sql server) - for example metadata
describing columns in a table. This metadata includes information about the
datatype of the column - for example "decimal", "small int", "varchar",
"image", "char" and so on.

Is there some built-in functionality in .net for converting these datatypes
to c# datatypes? For example, "varchar" could map to System.string. Or do I
need to write my own type-mapping method?


Thanks,
Peter
 
M

Marc Gravell

For most DAL purposes you know the type (per column) in advance, since you
designed the database; for more generic usage, you can always use
GetFieldType() and GetValue() on any IDataReader.

Would that do?

Marc
 
P

Peter Kirk

Marc Gravell said:
For most DAL purposes you know the type (per column) in advance, since you
designed the database; for more generic usage, you can always use
GetFieldType() and GetValue() on any IDataReader.

Would that do?

Not quite (I don't think), because I get metadata about a database column:

name
default value
is nullable
data type

ie, I get an IDataReader which contains some "string columns", which provide
information about a column in a database table.

One of the items of data describing the column is a string value which gives
the data type of the column. For example, this string value could be
"varchar" or "int".

I want to convert this string value to a c# datatype.



Peter
 
F

Frans Bouma [C# MVP]

Peter said:
Hi

I am reading metadata from a database (sql server) - for example
metadata describing columns in a table. This metadata includes
information about the datatype of the column - for example "decimal",
"small int", "varchar", "image", "char" and so on.

Is there some built-in functionality in .net for converting these
datatypes to c# datatypes? For example, "varchar" could map to
System.string. Or do I need to write my own type-mapping method?

There is no such method.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top