我有一个 MVC3 应用程序,我想将我的模型传递给一个构建命令对象的方法。原因是我有很多带有命令对象的方法,我希望代码写得更好。
private static SqlCommand CommandObj(vw_UserManager_Model model)
{
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
foreach (var item in model)
{
switch (property.PropertyType.Name)
{
case "String":
command.Parameters.Add("@" + property.Name, SqlDbType.VarChar).SqlValue = property;
break;
case "Guid":
command.Parameters.Add("@" + property.Name, SqlDbType.UniqueIdentifier).SqlValue = property;
break;
case "Int32":
command.Parameters.Add("@" + property.Name, SqlDbType.Int).SqlValue = property;
break;
case "Boolean":
//switch (property.Name.FirstOrDefault())
//{
// case true:
// command.Parameters.Add("@isactive", SqlDbType.Bit).SqlValue = 1;
// command.Parameters.Add("@isapproved", SqlDbType.Bit).SqlValue = 1;
// break;
// case false:
// command.Parameters.Add("@isactive", SqlDbType.Bit).SqlValue = 0;
// command.Parameters.Add("@isapproved", SqlDbType.Bit).SqlValue = 0;
// break;
//}
break;
}
}
return command;
}
目前这段代码无法编译,因为我无法像这样枚举我的模型。我想要做的是遍历模型中的每个项目并执行 switch 语句来构建正确的 dbType 参数。
有人对如何更改此代码有建议吗?
谢谢!!