Пример #1
0
        public string CreateSql <T>(T t)
        {
            string PrimaryName  = "";//主键名、值
            object ParmaryValue = null;

            Type   type      = typeof(T);
            string tableName = type.Name;

            StringBuilder sb = new StringBuilder();

            sb.Append("delete ");
            sb.Append(tableName);
            sb.Append(" where ");

            foreach (var pi in type.GetProperties())
            {
                if (pi != null)
                {
                    object value = pi.GetValue(t, null);//字段对应的值

                    object[] obj = pi.GetCustomAttributes(false);
                    if (obj.Count() > 0)
                    {
                        Col_Attribute ca = obj[0] as Col_Attribute;
                        if (ca.IsPrimaryKey)//主键
                        {
                            PrimaryName  = pi.Name;
                            ParmaryValue = value;
                            break;
                        }
                    }
                }
            }
            ParmaryValue = CommonFunc.UnInjection(ParmaryValue);
            if (string.IsNullOrEmpty(PrimaryName))
            {
                throw new Exception("需要主键(请检查表是否设置主键以及MODEL主键字段是否设置特性)");
            }
            else if (ParmaryValue == null)
            {
                throw new Exception("请检查主键值");
            }
            else
            {
                sb.Append(PrimaryName + "=" + ParmaryValue);
            }

            return(sb.ToString());
        }
Пример #2
0
        public string CreateSql <T>(T t)
        {
            Type          type      = typeof(T);
            string        tableName = type.Name;
            StringBuilder sb        = new StringBuilder();
            StringBuilder sb_v      = new StringBuilder();

            sb.Append("insert into ");
            sb.Append(tableName);
            sb.Append("(");

            sb_v.Append(" values (");
            foreach (var pi in type.GetProperties())
            {
                if (pi != null)
                {
                    sb.Append(pi.Name + ",");
                    object value = pi.GetValue(t, null);
                    value = CommonFunc.UnInjection(value);
                    if (value != null &&
                        (value.GetType() == typeof(int) || value.GetType() == typeof(decimal) || value.GetType() == typeof(double) || value.GetType() == typeof(long) || value.GetType() == typeof(float)))
                    {
                        sb_v.Append(value + ",");
                    }
                    else if (value.GetType() == typeof(DateTime))
                    {
                        sb_v.Append("to_date('" + value + "', 'YYYY-MM-DD HH24:MI:SS'),");
                    }
                    else
                    {
                        sb_v.Append("'" + value + "',");
                    }
                }
            }
            sb.Remove(sb.Length - 1, 1);
            sb.Append(")");

            sb_v.Remove(sb_v.Length - 1, 1);
            sb_v.Append(")");

            sb.Append(sb_v);

            return(sb.ToString());
        }
Пример #3
0
 public string CreateSql <T>(List <T> t)
 {
     return(CommonFunc.GetSql <T>(t, this));
 }
Пример #4
0
        public string CreateSql <T>(T t)
        {
            string PrimaryName  = "";//主键名、值
            object ParmaryValue = null;

            Type   type      = typeof(T);
            string tableName = type.Name;


            StringBuilder sb   = new StringBuilder();
            StringBuilder sb_u = new StringBuilder();

            sb.Append("update ");
            sb.Append(tableName);
            sb.Append(" set ");

            foreach (var pi in type.GetProperties())
            {
                if (pi != null)
                {
                    object value = pi.GetValue(t, null);          //字段对应的值
                    #region 主键
                    object[] obj = pi.GetCustomAttributes(false); //取特性
                    if (obj.Count() > 0)
                    {
                        Col_Attribute ca = obj[0] as Col_Attribute;
                        if (ca.IsPrimaryKey)//主键
                        {
                            PrimaryName  = pi.Name;
                            ParmaryValue = value;
                            continue;
                        }
                    }
                    #endregion
                    if (value == null)
                    {
                        continue;                //如果该字段为null不更新
                    }
                    value = CommonFunc.UnInjection(value);
                    sb_u.Append(pi.Name + "=");
                    if (value != null &&
                        (value.GetType() == typeof(int) || value.GetType() == typeof(decimal) || value.GetType() == typeof(double) || value.GetType() == typeof(long) || value.GetType() == typeof(float)))
                    {
                        sb_u.Append(value + ",");
                    }
                    else
                    {
                        sb_u.Append("'" + value + "',");
                    }
                }
            }

            if (string.IsNullOrEmpty(PrimaryName))
            {
                throw new Exception("需要主键(请检查表是否设置主键以及MODEL主键字段是否设置特性)");
            }
            else if (ParmaryValue == null)
            {
                throw new Exception("请检查主键值");
            }
            else if (string.IsNullOrEmpty(sb_u.ToString()))
            {
                throw new Exception("无更新");
            }
            else
            {
                sb_u.Remove(sb_u.Length - 1, 1);
                sb.Append(sb_u);
                sb.Append(" where ");
                sb.Append(PrimaryName + " = " + ParmaryValue);
            }

            return(sb.ToString());
        }