I'm working on a currency conversion project, now I've built a little script to pull the conversion rates and descriptions out of my database, but I can't seem to find a way to pull the variable (Rate) out of the string the datareader has created.
Here is the code snippet:
if (reader.HasRows)
//The reader will only read the rows if the ISO code matches the options avalible within the DB
{
Console.WriteLine("Result Found!");
while (reader.Read())
{
Console.WriteLine("Rate: {0}\t Conversion Rate: {1}",
reader[0], reader[1]);
}
reader.Close();
}
Now I want the Rate variable which is a decimal without having to convert it or shove it in an adapter (I'm new at C#).
tl;dr I want the output from "Rate: {0}" alone so I can use it to convert the currency's.
Any ideas?
have you tried
reader.ReadField<type>("fieldname") ?
EDIT: Wow, I can't believe I forgot that this was not part of SqlDataReader :p, I was just looking at my implementation... code for this:
Extension:
public static T ReadField<T>(this IDataReader reader, String fieldname)
{
return DB.ReaderField<T>(reader, fieldname);
}
DB.ReaderField:
public static T ReaderField<T>(IDataReader reader, String fieldname)
{
try
{
int idx = reader.GetOrdinal(fieldname);
if (reader.IsDBNull(idx))
{
return (T)default(T);
}
else
{
object o = reader.GetValue(idx);
try
{
return (T)Convert.ChangeType(o, typeof(T));
}
catch
{
return (T)default(T);
}
}
}
catch { }
return (T)default(T);
}
Sorry about the confusion :)