• C# ModelToSql


    [C# ModelToSql http://discuz.tangjunfei.com/forum.php?mod=viewthread&tid=4 (出处: TangZhiZzz!)](C# ModelToSql http://discuz.tangjunfei.com/forum.php?mod=viewthread&tid=4 (出处: TangZhiZzz!))

    点击查看代码
        public class ModelToSql
        {
            public static string CreateSelectSql<T>(T mdoel)
            {
                return "Select * From " + GetTableName<T>();
    
            }
            public static string CreateInsertSql<T>(T mdoel)
            {
                Type type = typeof(T);
    
                string[] pName = type.GetProperties().Where(m => !m.Name.Equals("ID")).Select(m => "[" + m.Name + "]").ToArray();
                string strSqlName = string.Join(",", pName);
                string[] pValue = type.GetProperties().Where(m => !m.Name.Equals("ID")).Select(m => "[" + m.GetValue(mdoel) + "]").ToArray();
                string strSqlValue = string.Join(",", pValue);
                return "insert into " + GetTableName<T>() + " ( " + strSqlName + " ) values (" + strSqlValue + ")";
    
            }
    
            public static string CreateUpdateSql<T>(T mdoel)
            {
                Type type = typeof(T);
                bool isHasID = type.GetProperties().Where(m => m.Name.Equals("ID")).Count() > 0 ? true : false;
                if (!isHasID) return "model not find ID";
                int ID = (int)type.GetProperties().Where(m => m.Name.Equals("ID")).FirstOrDefault().GetValue(mdoel);
                string[] pKV = type.GetProperties().Where(m => !m.Name.Equals("ID")).Select(m => m.Name + "=" + GetPropertyInfoValue(m, mdoel)).ToArray();
                string strSqlKV = string.Join(",", pKV);
    
                return "UPDATE " + GetTableName<T>() + " Set " + strSqlKV + " Where ID = " + ID;
    
            }
            public static string GetPropertyInfoValue<T>(PropertyInfo propertyInfo, T model)
            {
                string type = propertyInfo.PropertyType.Name;
                string str = string.Empty;
                switch (type)
                {
                    case "String":
                        str = "'" + propertyInfo.GetValue(model).ToString() + "'";
                        break;
                    case "DateTime":
                        str = "'" + propertyInfo.GetValue(model).ToString() + "'";
                        break;
                    case "Boolean":
                        str = propertyInfo.GetValue(model).ToString();
                        break;
                    default:
                        break;
                }
                return str;
    
            }
            public static T GetAttributeForModel<T>(Type type) where T : Attribute
            {
                var attrs = type.GetCustomAttributes(typeof(T), true);
                if (attrs.Any())
                {
                    return (T)attrs.Where(m => m.GetType() == typeof(T)).FirstOrDefault();
                }
                return null;
            }
            public static string GetTableName<T>()
            {
                var Table = GetAttributeForModel<TableNameAttribute>(typeof(T));
                if (Table != null)
                {
                    if (!string.IsNullOrWhiteSpace(Table.TableName))
                    {
                        return Table.TableName;
                    }
                }
                return typeof(T).Name;
            }
        }
    
  • 相关阅读:
    iOS开发第三方库一 IQKeyboardManager
    跟着百度学PHP[14]-初识PDO数据库抽象层
    文件上传漏洞的一些总结
    逻辑漏洞挖掘入门之 简单的任意账户密码重置
    突破MIME限制上传
    关于Safe DOG的文件上传bypass
    跟着百度学PHP[13]-文件上传
    PHP flock() 函数 php中的文件锁定机制
    mysql变量
    一份不错的php面试题(附答案)(笔试题)
  • 原文地址:https://www.cnblogs.com/tangzhizeishuai/p/15512405.html
Copyright © 2020-2023  润新知