Пример #1
0
 //删除数据
 public bool Delete <T>(object id)
 {
     try
     {
         Type           type       = typeof(T);
         string         table      = AttributeProcess.GetTableName(type);
         string         sql        = "DELETE FROM " + table + " WHERE ";
         PropertyInfo[] properties = type.GetProperties();
         foreach (PropertyInfo property in properties)
         {
             if (AttributeProcess.IsPrimary(type, property))
             {
                 sql += (AttributeProcess.GetColumnName(property) + "=");
                 if (property.PropertyType.IsPrimitive)
                 {
                     sql += (id.ToString() + ";");
                 }
                 else
                 {
                     sql += ("\'" + id.ToString() + "\';");
                 }
             }
         }
         ExecuteQuery(sql);
         return(true);
     }
     catch (Exception e)
     {
         throw e;
     }
     finally
     {
         CloseSqlConnection();
     }
 }
Пример #2
0
        //public ORMHelper<T> Where(object field, string op = null, string condition = null)//查询逻辑 and or xor
        //{
        //    this.ParseWhereExp("AND", field, op, condition);
        //    return this;
        //}

        //public ORMHelper<T> WhereOr(object field, string op = null, string condition = null)
        //{
        //    this.ParseWhereExp("OR", field, op, condition);
        //    return this;
        //}

        //protected void ParseWhereExp(string logic, object field, string op, string condition)
        //{
        //    if (field is string)
        //    {
        //        WhereParam wp = new WhereParam
        //        {
        //            logic = logic,
        //            filed = field.ToString(),
        //            op = op,
        //            condition = condition
        //        };
        //        conditions.Enqueue(wp);
        //    }
        //    else if (field is Dictionary<string, string>)
        //    {
        //        foreach (var item in (Dictionary<string, string>)field)
        //        {
        //            WhereParam wp = new WhereParam
        //            {
        //                logic = logic,
        //                filed = item.Key,
        //                op = "=",
        //                condition = item.Value
        //            };
        //            conditions.Enqueue(wp);
        //        }
        //    }
        //}

        ///// <summary>
        ///// 创建子查询SQL
        ///// </summary>
        ///// <returns></returns>
        //public string BuildSql()
        //{
        //    string sql = string.Empty,
        //        str_fileds = string.Empty,
        //        str_where = string.Empty;
        //    while (conditions.Count > 0)
        //    {
        //        WhereParam wp = conditions.Dequeue();
        //        if (string.IsNullOrEmpty(str_where))
        //        {
        //            str_where = string.Format("where {0}{1}{2}",wp.filed,wp.logic);
        //        }
        //    }
        //    return sql;
        //}
        #endregion

        /// <summary>
        /// 查找单条记录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql">sql指令</param>
        /// <returns></returns>
        public T Find <T>(string sql)
        {
            //if (string.IsNullOrEmpty(sql)) sql = this.BuildSql();
            DbDataReader reader = DbHelper.GetDataReader(sql);
            Type         type   = typeof(T);

            try
            {
                if (type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type.IsEnum)
                {
                    if (type.IsEnum)
                    {
                        return((T)Enum.ToObject(type, reader.GetValue(0)));
                    }
                    else
                    {
                        return((T)Convert.ChangeType(reader.GetValue(0), type));
                    }
                }
                else
                {
                    T result = Activator.CreateInstance <T>();
                    PropertyInfo[] properyies = type.GetProperties();
                    string         columName;
                    foreach (PropertyInfo property in properyies)
                    {
                        columName = AttributeProcess.GetColumnName(property);
                        if (!ReaderExists(reader, columName))
                        {
                            continue;
                        }
                        var value = reader.GetValue(reader.GetOrdinal(columName));
                        if (property.PropertyType.IsPrimitive && value.Equals(DBNull.Value))
                        {
                            value = Activator.CreateInstance(property.PropertyType);
                        }
                        if (property.PropertyType.IsEnum)
                        {
                            property.SetValue(result, Enum.ToObject(property.PropertyType, value), null);
                        }
                        else
                        {
                            property.SetValue(result, Convert.ChangeType(value, property.PropertyType), null);
                        }
                    }
                    return(result);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(sql, ex);
            }
            finally
            {
                reader.Close();
            }
        }
Пример #3
0
 //插入新数据
 public bool Insert <T>(T data)
 {
     try
     {
         Type           type       = data.GetType();
         string         table      = AttributeProcess.GetTableName(type);
         List <string>  columns    = new List <string>();
         List <string>  values     = new List <string>();
         PropertyInfo[] properties = type.GetProperties();
         foreach (PropertyInfo property in properties)
         {
             if (!(AttributeProcess.IsPrimary(type, property) && AttributeProcess.IsIncrement(type)))
             {
                 if (property.GetValue(data, null) != null)
                 {
                     columns.Add(AttributeProcess.GetColumnName(property));
                     if (property.PropertyType == typeof(bool))
                     {
                         bool value = bool.Parse(property.GetValue(data, null).ToString());
                         values.Add((value ? "1" : "0"));
                     }
                     else if (property.PropertyType.IsPrimitive)
                     {
                         values.Add(property.GetValue(data, null).ToString());
                     }
                     else if (property.PropertyType.IsEnum)
                     {
                         int intValue = (int)property.GetValue(data, null);
                         values.Add(intValue.ToString());
                     }
                     else
                     {
                         if (Sql.InjectionDefend(property.GetValue(data, null).ToString()))
                         {
                             values.Add("\'" + property.GetValue(data, null) + "\'");
                         }
                     }
                 }
             }
         }
         string sql = "INSERT INTO " + table + "(" + string.Join(",", columns) + ")" + "VALUES" + "(" + string.Join(",", values) + ")";
         ExecuteQuery(sql);
         return(true);
     }
     catch (Exception e)
     {
         throw e;
     }
     finally
     {
         CloseSqlConnection();
     }
 }
Пример #4
0
 //查询所有符合条件的记录
 public List <T> Fetch <T>(Sql sql)
 {
     try
     {
         ExecuteQuery(sql.GetSql());
         List <T> list = new List <T>();
         Type     type = typeof(T);
         if (type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type.IsEnum)
         {
             while (reader.Read())
             {
                 if (type.IsEnum)
                 {
                     list.Add((T)Enum.ToObject(type, reader.GetValue(0)));
                 }
                 else
                 {
                     list.Add((T)Convert.ChangeType(reader.GetValue(0), type));
                 }
             }
         }
         else
         {
             while (reader.Read())
             {
                 T result = Activator.CreateInstance <T>();
                 PropertyInfo[] properties = type.GetProperties();
                 foreach (PropertyInfo property in properties)
                 {
                     string columName = AttributeProcess.GetColumnName(property);
                     if (property.PropertyType.IsEnum)
                     {
                         property.SetValue(result, Enum.ToObject(property.PropertyType, reader.GetValue(reader.GetOrdinal(columName))), null);
                     }
                     else
                     {
                         property.SetValue(result, Convert.ChangeType(reader.GetValue(reader.GetOrdinal(columName)), property.PropertyType), null);
                     }
                 }
                 list.Add(result);
             }
         }
         return(list);
     }
     catch (Exception e)
     {
         throw e;
     }
     finally
     {
         CloseSqlConnection();
     }
 }
Пример #5
0
 //更新指定的记录
 public bool Update <T>(T data)
 {
     try
     {
         Type          type  = data.GetType();
         string        table = AttributeProcess.GetTableName(type);
         string        sql   = "Update " + table + " Set ";
         List <string> sets  = new List <string>();
         string where = " Where ";
         PropertyInfo[] properties = type.GetProperties();
         foreach (PropertyInfo property in properties)
         {
             string column = AttributeProcess.GetColumnName(property);
             if (!AttributeProcess.IsPrimary(type, property))
             {
                 if (property.PropertyType == typeof(bool))
                 {
                     bool value = bool.Parse(property.GetValue(data, null).ToString());
                     sets.Add(column + "=" + (value ? "1" : "0"));
                 }
                 else if (property.PropertyType.IsPrimitive)
                 {
                     sets.Add(column + "=" + property.GetValue(data, null));
                 }
                 else if (property.PropertyType.IsEnum)
                 {
                     int intValue = (int)property.GetValue(data, null);
                     sets.Add(column + "=" + intValue);
                 }
                 else
                 {
                     if (Sql.InjectionDefend(property.GetValue(data, null).ToString()))
                     {
                         sets.Add(column + "=\'" + property.GetValue(data, null) + "\'");
                     }
                 }
             }
             else
             {
                 if (property.PropertyType.IsPrimitive)
                 {
                     where += column + "=" + property.GetValue(data, null);
                 }
                 else
                 {
                     where += column + "=\'" + property.GetValue(data, null) + "\'";
                 }
             }
         }
         sql += (string.Join(",", sets) + where);
         ExecuteQuery(sql);
         return(true);
     }
     catch (Exception e)
     {
         throw e;
     }
     finally
     {
         CloseSqlConnection();
     }
 }