Until now i got SQL-results from a datareader and stored it in a collection, all variables were strings:
AdressEntries client = new AdressEntries();
Type t = client.GetType();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
client = new AdressEntries();
foreach (PropertyInfo variable in t.GetProperties())
{
variable.SetValue(client, reader[variable.Name].ToString());
}
allClients.Add(client);
}
reader.Close();
this worked great for all kind of collections. Now i want to use more datatypes in the app and in the SQL Database (like date, int, double etc.) The variables used in the collections are nullable (like int?, Boolean?, etc.).
Now i have to cast/convert the SQL-Results. I did this with a little correction to the above code with Convert.ChangeType:
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
client = new AdressEntries();
foreach (PropertyInfo variable in t.GetProperties())
{
variable.SetValue(client, Convert.ChangeType(reader[variable.Name], variable.GetType()));
}
allClients.Add(client);
}
reader.Close();
Can I do this, or do I run into problems?
Aucun commentaire:
Enregistrer un commentaire