当前位置: 代码迷 >> SQL >> 经过反射写SQL
  详细解决方案

经过反射写SQL

热度:36   发布时间:2016-05-05 13:08:32.0
通过反射写SQL
   private string Trans2Sql(T t, dbOperType dot)        {            Type type = t.GetType();            string result = "";            PropertyInfo[] properties = type.GetProperties();            StringBuilder sb = new StringBuilder();            StringBuilder sIn = new StringBuilder();            foreach (PropertyInfo field in properties)            {                switch (dot)                {                    case dbOperType.Select:                        sb.Append(field.Name).Append(",");                        break;                    case dbOperType.Insert:                        sb.Append(field.Name).Append(",");                        sIn.Append(DBUtils.GetSqlString(field.PropertyType, field.GetValue(t, null))).Append(",");                        break;                    case dbOperType.Update:                        sb.Append(field.Name).Append("=")                            .Append(DBUtils.GetSqlString(field.PropertyType, field.GetValue(t, null)))                            .Append(",");                        break;                    case dbOperType.Delete:                        break;                }            }            if (sb.Length > 0)                sb.Remove(sb.Length - 1, 1);            if (sIn.Length > 0)                sIn.Remove(sIn.Length - 1, 1);            switch (dot)            {                case dbOperType.Select:                    result = "select " + sb.ToString() + " from " + type.Name;                    break;                case dbOperType.Insert:                    result = "Insert into " + type.Name + "(" + sb.ToString() + ")";                    result += " Values(" + sIn.ToString() + ")";                    break;                case dbOperType.Update:                    result = "Update " + type.Name + " set " + sb.ToString();                    break;                case dbOperType.Delete:                    result = "Delete from " + type.Name;                    break;            }            return result;        }

2011-6-14 22:16 danny
  相关解决方案