예제 #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 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();
     }
 }
예제 #3
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();
     }
 }