I have a model with some properties. I have a database table with the exact same names for the fields as the model properties. For example, table User has ID, FirstName, LastName, and the model has the same fields. When I retrieve a particular user I get a datarow of values. Is there a way to automatically fill the model from the datarow without having to specifically set each property?
public static Object FillModel(DataRow dr, Object model)
{
foreach (PropertyInfo prop in model.GetType().GetProperties())
{
string name = prop.Name;
string value = dr[name].ToString();
//prop.SetValue(...
}
}
Plus, I may need to convert data based on the type, whether string or integer or date.
Aucun commentaire:
Enregistrer un commentaire