我正在尝试在给定键值列表的表中填充行
使用DataContext.Mapping我能够找到正确的表(给定一个表名)并创建一行。
// Look up the table
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");
}
然后我迭代行属性并填充值。
// Iterate through the dictionary and try to match up the keys with column names
foreach (KeyValuePair<string, string> listItem in list)
{
PropertyInfo propertyInfo = rowType.GetProperty(listItem.Key);
if (propertyInfo == null)
{
throw new Exception("Invalid column name specified");
}
// Set the value on the object
try
{
propertyInfo.SetValue(row, Convert.ChangeType(listItem.Value, propertyInfo.PropertyType), null);
}
catch
{
throw new Exception("Value specified cannot be converted to database type");
}
}
我现在需要将此行对象插回到DB中。我一直在玩db.GetTable<rowType>();
而没有运气。感谢
答案 0 :(得分:1)
我正在思考它
db.GetTable(rowType).InsertOnSubmit(row);
db.SubmitChanges();