SqlDataReader Type Conversion

  • Thread starter Thread starter T!LT3D
  • Start date Start date
T

T!LT3D

Hi

I'm trying to convert data automatically from a SqlDataReader into the
correct type for its class property, it goes something like this but is
reported syntactilly incorrect....

MyField = (_sr.GetFieldType(i))_sr.GetValue(i)

....it complains at the second _sr (; expected), is there a nice funky way of
doing this without creating some kind of conversion matrix i.e.....

if _sr.GetFieldType(i) = "aType" MyField = (aType)_sr.GetValue(i)
else if _sr.GetFieldType(i) = "bType" MyField = (bType)_sr.GetValue(i)
else if ......

Any help appreciated.

Chris.
 
...
I'm trying to convert data automatically from
a SqlDataReader into the correct type for its
class property, it goes something like this but is
reported syntactilly incorrect....

MyField = (_sr.GetFieldType(i))_sr.GetValue(i)

It's not only syntactically incorrect, it's logically incorrect as well.

In the sentence above it doesn't really seem that you're trying to "convert"
the value, but rather that you're trying to "cast" it, which is a completely
other cup of tea.

When you cast in that manner, you have to use the "identifier" of the class
you're casting to, not an "instance" of the Type-class.
...it complains at the second _sr (; expected),
is there a nice funky way of doing this without
creating some kind of conversion matrix i.e...

The second thing about your question is whether you really need to cast or
convert it. What is the type of MyField? If it's of the type "object", the
cast shouldn't really be needed, unless you're missing to give us some other
information.

An example of what you can do is to just get the value:

object MyField = _sr.GetValue(i);
if _sr.GetFieldType(i) =
"aType" MyField =
(aType)_sr.GetValue(i)
else if _sr.GetFieldType(i) =
"bType" MyField =
(bType)_sr.GetValue(i)
else if ......

Any help appreciated.

It is *not* possible to change the Type of a variable in the way you're
indicating in the code above.

It seems like you're coming in from the VB-side, as what you have written
indicates some "Variant"-type, the use of "=" in comparisons, as well as the
exclusion of ";" at the end of statements.

My suggestion is that you look into more of object-orientation, the
typesystem and polymorphism, and how that can be used with the class
"System.Object" as the root-class.

Possibly you also could make your question more clear as to *why* you want
to "convert" or cast the value, i.e. how you're going to use the variable
MyField in the further context.

// Bjorn A
 
Thanks for your response Bjorn,

I have a class with various properties, by dynamically building a select
query using reflection and attributes I was able to pull the necessary
fields from SQL, what I didn't quite know how to do (at the time) was set
the values of the properties to the fields pulled from SQL, this I have
solved now using....

public void ApplyValue(object ob, string fieldName, object fieldValue)
{
Type t = ob.GetType();
foreach (PropertyInfo p in t.GetProperties())
{
foreach (Attribute att in p.GetCustomAttributes(false))
{
DBColumn dca = att as DBColumn;
if (dca != null)
{
if (dca.ColumnName == fieldName)
{
p.SetValue(ob, fieldValue , null);
return;
}
}
}
}
}

.....apologies for the lack of ";" and use of the "=" operator in my earlier
post, fortunately I not only come from a VB background but also a Assembly,
Cobol, Basic, Pascal, C, Perl (to name but a few) and therefore tend to
write more pseudo when defining functionality rather writing language
specific code.

Problem solved now anyway.

Chris.
 
Back
Top