I am trying to fill a row in a table given a list of key values
Using DataContext.Mapping I can find the correct table (given the name of the table) and create a row.
MetaTable matchedTable = null;
foreach (MetaTable tableMetaData in db.Mapping.GetTables())
{
if (table.Equals(tableMetaData.TableName))
{
matchedTable = tableMetaData;
break;
}
}
if (matchedTable == null)
{
throw new Exception("Invalid table name specified");
}
Then I repeat the properties of the string and fill in the values.
foreach (KeyValuePair<string, string> listItem in list)
{
PropertyInfo propertyInfo = rowType.GetProperty(listItem.Key);
if (propertyInfo == null)
{
throw new Exception("Invalid column name specified");
}
try
{
propertyInfo.SetValue(row, Convert.ChangeType(listItem.Value, propertyInfo.PropertyType), null);
}
catch
{
throw new Exception("Value specified cannot be converted to database type");
}
}
Now I need to return this row object back to the database. I have not played with db.GetTable<rowType>();. Thanks
source
share