时间:2021-07-01 10:21:17 帮助过:28人阅读
如果不需要添加为null或者为空的栏位,则可以使用如下代码
public string GetInsertSqlByModel(Object o) { StringBuilder sbStart = new StringBuilder(); StringBuilder sbFields = new StringBuilder(); StringBuilder sbValues = new StringBuilder(); Type t = o.GetType(); string modelName = t.Name; foreach (PropertyInfo p in t.GetProperties()) { if (p.GetValue(o) == null||string.IsNullOrEmpty(p.GetValue(o).ToString())) { continue; } sbFields.Append(p.Name + ","); sbValues.AppendFormat("‘{0}‘,",p.GetValue(o)); } sbStart.AppendFormat("insert into {0}({1}) values({2})",modelName,sbFields.ToString().TrimEnd(‘,‘), sbValues.ToString().TrimEnd(‘,‘)); return sbStart.ToString(); } }
利用反射将Model转化为sql
标签:tsql blog foreach color col span type UI star