中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

EF架(jia)構~一(yi)個(ge)規范,兩個(ge)實現(續)~性能可以接(jie)受(shou)的批(pi)量增(zeng)刪改操作

回到目錄

上回主要把數據操作規范及EF兩種基類下的實現說了一下,今(jin)天主要針對ObjectContext情況下(xia)的(de)(de)(de)批(pi)量操作(zuo)(zuo)作(zuo)(zuo)一(yi)(yi)個(ge)詳細的(de)(de)(de)說明,首先,要說明一(yi)(yi)點(dian),批(pi)量操作(zuo)(zuo)不用ObjectContext提供(gong)的(de)(de)(de)方法,而是使用拼SQL串(chuan)(chuan)的(de)(de)(de)方式(shi),將列(lie)表(biao)拼成一(yi)(yi)個(ge)SQL串(chuan)(chuan),一(yi)(yi)次的(de)(de)(de)發給數(shu)據庫(ku),這樣在性能上絕對是一(yi)(yi)個(ge)質的(de)(de)(de)飛越,本人(ren)嘗試過,無(wu)論是EF還是linq to sql,它們提供(gong)的(de)(de)(de)方法,對于(yu)列(lie)表(biao)的(de)(de)(de)操作(zuo)(zuo)會產生(sheng)N條SQL串(chuan)(chuan)(N=列(lie)表(biao)的(de)(de)(de)長度)。

一個操作類型枚舉:

1         /// <summary>
2         /// 執行(xing)SQL的類型(xing)
3         /// </summary>
4         public enum SQLType
5         {
6             Insert,
7             Update,
8             Delete,
9         }

調用方法很容易:

public void Insert<TEntity>(List<TEntity> list) where TEntity : class
        {
            DoListSQL(list, SQLType.Insert);
        }

public void Update<TEntity>(List<TEntity> list) where TEntity : class
        {
            DoListSQL<TEntity>(list, SQLType.Update);
        }

  public void Delete<TEntity>(List<TEntity> list) where TEntity : class
        {
            DoListSQL<TEntity>(list, SQLType.Delete);
        }

下(xia)面是(shi)實(shi)現這樣方法的(de)代碼,即SQL串在ObjectContext環境下(xia)是(shi)如何拼接的(de):

  1  /// <summary>
  2         /// 構建Update語(yu)句(ju)串
  3         /// </summary>
  4         /// <typeparam name="TEntity"></typeparam>
  5         /// <param name="entity"></param>
  6         /// <returns></returns>
  7         private Tuple<string, object[]> CreateUpdateSQL<TEntity>(TEntity entity) where TEntity : class
  8         {
  9             if (entity == null)
 10                 throw new ArgumentException("The database entity can not be null.");
 11 
 12             Type entityType = entity.GetType();
 13             var table = entityType.GetProperties().Where(i => i.PropertyType != typeof(EntityKey) && i.PropertyType != typeof(EntityState)).ToArray();
 14             var primaryKeyColumns = (entity as EntityObject).GetPK();
 15             if (primaryKeyColumns == null || primaryKeyColumns.Count == 0)
 16                 throw new ArgumentException("The Table entity have not a primary key.");
 17             List<object> arguments = new List<object>();
 18             StringBuilder builder = new StringBuilder();
 19 
 20             foreach (var change in table)
 21             {
 22                 if (primaryKeyColumns.Contains(change))
 23                     continue;
 24 
 25                 if (arguments.Count != 0)
 26                     builder.Append(", ");
 27 
 28                 if (change.GetValue(entity, null) != null)
 29                 {
 30                     builder.Append(change.Name + " = {" + arguments.Count + "}");
 31 
 32                     if (change.PropertyType == typeof(string) || change.PropertyType == typeof(DateTime))
 33                         arguments.Add("'" + change.GetValue(entity, null).ToString().Replace("'", "char(39)") + "'");
 34                     else
 35                         arguments.Add(change.GetValue(entity, null));
 36                 }
 37                 else
 38                 {
 39                     builder.Append(change.Name + " = NULL, ");
 40                 }
 41             }
 42 
 43             if (builder.Length == 0)
 44                 throw new Exception("沒有任何屬性進(jin)行更新");
 45 
 46             builder.Insert(0, " UPDATE " + string.Format("[{0}]", entityType.Name) + " SET ");
 47 
 48             builder.Append(" WHERE ");
 49             bool firstPrimaryKey = true;
 50 
 51             foreach (var primaryField in primaryKeyColumns)
 52             {
 53                 if (firstPrimaryKey)
 54                     firstPrimaryKey = false;
 55                 else
 56                     builder.Append(" AND ");
 57 
 58                 object val = entityType.GetProperty(primaryField.Name).GetValue(entity, null);
 59                 builder.Append(GetEqualStatment(primaryField.Name, arguments.Count));
 60                 arguments.Add(val);
 61             }
 62             return new Tuple<string, object[]>(builder.ToString(), arguments.ToArray());
 63 
 64         }
 65 
 66         /// <summary>
 67         /// 構建Insert語(yu)句(ju)串(chuan)
 68         /// 主鍵(jian)為(wei)自增時,如果主鍵(jian)值為(wei)0,我們將主鍵(jian)插入(ru)到(dao)SQL串中
 69         /// </summary>
 70         /// <typeparam name="TEntity"></typeparam>
 71         /// <param name="entity"></param>
 72         /// <returns></returns>
 73         private Tuple<string, object[]> CreateInsertSQL<TEntity>(TEntity entity) where TEntity : class
 74         {
 75             if (entity == null)
 76                 throw new ArgumentException("The database entity can not be null.");
 77 
 78             Type entityType = entity.GetType();
 79             var table = entityType.GetProperties().Where(i => i.PropertyType != typeof(EntityKey) && i.PropertyType != typeof(EntityState)).ToArray();
 80             var primaryKeyColumns = (entity as EntityObject).GetPK();
 81 
 82             List<object> arguments = new List<object>();
 83             StringBuilder fieldbuilder = new StringBuilder();
 84             StringBuilder valuebuilder = new StringBuilder();
 85 
 86             fieldbuilder.Append(" INSERT INTO " + string.Format("[{0}]", entityType.Name) + " (");
 87 
 88             foreach (var member in table)
 89             {
 90                 if (primaryKeyColumns.Contains(member) && Convert.ToString(member.GetValue(entity, null)) == "0")
 91                     continue;
 92                 object value = member.GetValue(entity, null);
 93                 if (value != null)
 94                 {
 95                     if (arguments.Count != 0)
 96                     {
 97                         fieldbuilder.Append(", ");
 98                         valuebuilder.Append(", ");
 99                     }
100 
101                     fieldbuilder.Append(member.Name);
102                     if (member.PropertyType == typeof(string) || member.PropertyType == typeof(DateTime))
103                         valuebuilder.Append("'{" + arguments.Count + "}'");
104                     else
105                         valuebuilder.Append("{" + arguments.Count + "}");
106                     if (value.GetType() == typeof(string))
107                         value = value.ToString().Replace("'", "char(39)");
108                     arguments.Add(value);
109 
110                 }
111             }
112 
113 
114             fieldbuilder.Append(") Values (");
115 
116             fieldbuilder.Append(valuebuilder.ToString());
117             fieldbuilder.Append(");");
118             return new Tuple<string, object[]>(fieldbuilder.ToString(), arguments.ToArray());
119         }
120         /// <summary>
121         /// 構建Delete語句串
122         /// </summary>
123         /// &lt;typeparam name="TEntity"></typeparam>
124         /// <param name="entity"></param>
125         /// <returns></returns>
126         private Tuple<string, object[]> CreateDeleteSQL<TEntity>(TEntity entity) where TEntity : class
127         {
128             if (entity == null)
129                 throw new ArgumentException("The database entity can not be null.");
130 
131             Type entityType = entity.GetType();
132             var table = entityType.GetProperties().Where(i => i.PropertyType != typeof(EntityKey) && i.PropertyType != typeof(EntityState)).ToArray();
133             var primaryKeyColumns = (entity as EntityObject).GetPK();
134             if (primaryKeyColumns == null || primaryKeyColumns.Count == 0)
135                 throw new ArgumentException("The Table entity have not a primary key.");
136 
137             List<object> arguments = new List<object>();
138             StringBuilder builder = new StringBuilder();
139             builder.Append(" Delete from " + string.Format("[{0}]", entityType.Name));
140 
141             builder.Append(" WHERE ");
142             bool firstPrimaryKey = true;
143 
144             foreach (var primaryField in primaryKeyColumns)
145             {
146                 if (firstPrimaryKey)
147                     firstPrimaryKey = false;
148                 else
149                     builder.Append(" AND ");
150 
151                 object val = entityType.GetProperty(primaryField.Name).GetValue(entity, null);
152                 builder.Append(GetEqualStatment(primaryField.Name, arguments.Count));
153                 arguments.Add(val);
154             }
155             return new Tuple<string, object[]>(builder.ToString(), arguments.ToArray());
156         }
157 
158 
159         /// <summary>
160         /// 執(zhi)行實體列(lie)表以SQL串的方式
161         /// </summary>
162         /// <typeparam name="TEntity"></typeparam>
163         /// <param name="list"></param>
164         private void DoListSQL<TEntity>(IEnumerable<TEntity> list, SQLType sqlType) where TEntity : class
165         {
166             StringBuilder sqlstr = new StringBuilder();
167 
168             switch (sqlType)
169             {
170                 case SQLType.Insert:
171                     list.ToList().ForEach(i =>
172                    {
173                        Tuple<string, object[]> sql = CreateInsertSQL(i);
174                        sqlstr.AppendFormat(sql.Item1, sql.Item2);
175                    });
176                     break;
177                 case SQLType.Update:
178                     list.ToList().ForEach(i =>
179                     {
180                         Tuple<string, object[]> sql = CreateUpdateSQL(i);
181                         sqlstr.AppendFormat(sql.Item1, sql.Item2);
182                     });
183                     break;
184                 case SQLType.Delete:
185                     list.ToList().ForEach(i =>
186                    {
187                        Tuple<string, object[]> sql = CreateDeleteSQL(i);
188                        sqlstr.AppendFormat(sql.Item1, sql.Item2);
189                    });
190                     break;
191                 default:
192                     throw new ArgumentException("請(qing)輸入正確的參數(shu)");
193             }
194 
195             _db.ExecuteStoreCommand(sqlstr.ToString());
196         }

代碼在vs2010+sql2005+mvc3環境下測(ce)試通過(guo)

回到目錄

posted @ 2012-10-22 22:58  張占嶺  閱讀(3200)  評論(5)    收藏  舉報