zoukankan      html  css  js  c++  java
  • EF架构~性能高效的批量操作(Insert篇)

    回到目录

    无论是linq to sql 还是entity frameworks,在进行列表操作时都会有一个毛病,那就是它的操作只能一个实体一个实体的发到服务器,这样,如果列表的数量很大,如列表为10万条数据,那么,这种操作将是非常性能的,可能你的DB就挂了。

    解决方案:拼接T—SQL串,并使它具有通用性

    好处:与服务器建立一次连接,给服务器发一条SQL命令,即可实现

    代码如下:

     1   /// <summary>
     2         /// 构建Insert语句串
     3         /// 主键为自增时,如果主键值为0,我们将主键插入到SQL串中
     4         /// </summary>
     5         /// <typeparam name="TEntity"></typeparam>
     6         /// <param name="entity"></param>
     7         /// <returns></returns>
     8         private Tuple<string, object[]> CreateInsertSQL<TEntity>(TEntity entity) where TEntity : class
     9         {
    10             if (entity == null)
    11                 throw new ArgumentException("The database entity can not be null.");
    12 
    13             Type entityType = entity.GetType();
    14             var table = entityType.GetProperties().Where(i => i.PropertyType != typeof(EntityKey)
    15                && i.PropertyType != typeof(EntityState)
    16                && i.GetValue(entity, null) != null
    17                && (i.PropertyType.IsValueType || i.PropertyType == typeof(string)))
    18                .ToArray();//过滤主键,航行属性,状态属性等
    19             List<string> pkList = GetPrimaryKey<TEntity>().Select(i => i.Name).ToList();
    20 
    21             List<object> arguments = new List<object>();
    22             StringBuilder fieldbuilder = new StringBuilder();
    23             StringBuilder valuebuilder = new StringBuilder();
    24 
    25             fieldbuilder.Append(" INSERT INTO " + string.Format("[{0}]", entityType.Name) + " (");
    26 
    27             foreach (var member in table)
    28             {
    29                 if (pkList.Contains(member.Name) && Convert.ToString(member.GetValue(entity, null)) == "0")
    30                     continue;
    31                 object value = member.GetValue(entity, null);
    32                 if (value != null)
    33                 {
    34                     if (arguments.Count != 0)
    35                     {
    36                         fieldbuilder.Append(", ");
    37                         valuebuilder.Append(", ");
    38                     }
    39 
    40                     fieldbuilder.Append(member.Name);
    41                     if (member.PropertyType == typeof(string) || member.PropertyType == typeof(DateTime))
    42                         valuebuilder.Append("'{" + arguments.Count + "}'");
    43                     else
    44                         valuebuilder.Append("{" + arguments.Count + "}");
    45                     if (value.GetType() == typeof(string))
    46                         value = value.ToString().Replace("'", "char(39)");
    47                     arguments.Add(value);
    48 
    49                 }
    50             }
    51 
    52 
    53             fieldbuilder.Append(") Values (");
    54 
    55             fieldbuilder.Append(valuebuilder.ToString());
    56             fieldbuilder.Append(");");
    57             return new Tuple<string, object[]>(fieldbuilder.ToString(), arguments.ToArray());
    58         }

    之后我将陆续把更新操作与删除操作及对增删改操作进行封装,献给大家,尽请期待。

    回到目录

  • 相关阅读:
    图像控件 ImageControl
    日期条控件 DateFieldControl
    日期选择器和日期条控件 DateChooserAndDateFieldControls
    计数器控件实例 NumericStepper
    树结构控件实例 TreeControl
    vue2.0leaflet
    关于工具类中@Autowired注入为NULL的问题记录
    zabbix_agentd重装后启动时IPC和共享内存段问题
    rsync如何不指定密码文件
    MySQL5.7 JSON类型及其相关函数的学习
  • 原文地址:https://www.cnblogs.com/lori/p/2747217.html
Copyright © 2011-2022 走看看