c#自定義ORM框架---(泛型&反射&實體類擴展屬性<附帶通用增、刪、查、改>)

該教材主要是運用到泛型、反射和實體類擴展屬性

步驟1、創建擴展屬性類數據庫

實體類擴展屬性要繼承Attribute基類完成框架

 1 [AttributeUsage(AttributeTargets.Property)]
 2     public class FieldAttribute : Attribute
 3     {
 4         /// <summary>
 5         ///是否爲主鍵(true/false)
 6         /// </summary>
 7         public bool PropertyKey { get; set; }
 8         /// <summary>
 9         /// 是否爲自動增加(true/false)
10         /// </summary>
11         public bool Identity { get; set; }
12     }
1 [AttributeUsage(AttributeTargets.Class)]
2     public class TableAttribute:Attribute
3     {
4         /// <summary>
5         /// 數據庫表名
6         /// </summary>
7         public string TableName { get; set; }
8     }

步驟2、建立實體類並引用擴展實體類屬性函數

注意:
[Serializable]表示實體類可被序列化
[Table(TableName = "emesc.Dome")]表示引入的擴展屬性名而且數據庫名爲emesc.Dome
 
[Field(PropertyKey = true)]表示是否爲主鍵
 
 1 [Table(TableName = "emesc.Dome")]
 2     [Serializable]
 3     public class Dome
 4     {
 5         [Field(PropertyKey = true)]
 6         public string EMP_NO { get; set; }
 7         public string EMP_NAME { get; set; }
 8         public string EMP_DESC { get; set; }
 9         public string TYPE { get; set; }
10         public string EMP_RANK { get; set; }
11         public string EMP_PASS { get; set; }
12         public object FACTORY_CODE { get; set; }
13         public DateTime QUIT_DATE { get; set; }
14         public object CALENDAR_CODE { get; set; }
15     }

步驟3、建立BaseClass 該Class主要是用來封裝ORM框架的通用方法(增、刪、查、改)學習

  1.反射出實體類全部字段屬性ui

  注意:反射實體類字段主要運用到PropertyInfo[]和泛型Tspa

      PropertyInfo[] 主要反射實體類字段屬性.net

      T 主要是用來傳遞實體類對象code

  

 1 private static string GetClassType<T>(T modelName) {
 2             try {
 3             StringBuilder type= new StringBuilder();
 4             PropertyInfo[]  props = modelName.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//抓取泛型類的字段屬性;
 5             foreach ( PropertyInfo Info in props) {
 6                 //if ((Info.GetValue(modelName,null) + "").Equals("")|| (Info.GetValue(modelName, null) + "").Equals(null)) continue;
 7                 //if (Info.PropertyType.Name.StartsWith("DateTime"))
 8                 //{
 9                 //    if (Convert.ToDateTime(Info.GetValue(modelName, null)) == DateTime.MinValue) continue;//判斷時間是否為最小時間 ;
10                 //}
11                 type.Append(Info.Name+",");
12             }
13             return type.ToString().Substring(0, Start.Length - 1);//拼接全部字段屬性名
14             }
15             catch (Exception ex)
16             {
17                 throw ex;
18             }
19         }

  2.反射擴展類字段屬性對象

 1  /// <summary>
 2         /// 獲取實體類中的惟一id和對應的表名
 3         /// </summary>
 4         /// <typeparam name="T"></typeparam>
 5         /// <param name="modelName"></param>
 6         /// <returns></returns>
 7         public static List<string> GetModelFeature<T>(T modelName) {
 8             try {
 9             List<string> list = new List<string>();
10             var info= modelName.GetType().GetCustomAttributes(typeof(TableAttribute), false);
11             foreach (var item in info)
12             {
13                 TableAttribute attr = item as TableAttribute;
14                 if (attr != null)
15                 {
16                     string tableName = attr.TableName;//表名只有獲取一次
17                     list.Add(tableName);
18                     break;
19                 }
20             }
21             PropertyInfo[] props = modelName.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//抓取泛型類的字段屬性;
22             foreach (var item in props)
23             {
24                 object[] objAttrs = item.GetCustomAttributes(typeof(FieldAttribute), true);
25                 if (objAttrs.Length > 0)
26                 {
27                     FieldAttribute attr = objAttrs[0] as FieldAttribute;
28                     if (attr.PropertyKey == true)
29                     {
30                         list.Add(item.Name+" = '"+ item.GetValue(modelName,null)+"'");//抓取篩選條件
31                     }
32                 }
33             }
34             return list;
35             }
36             catch (Exception ex)
37             {
38                 throw ex;
39             }
40         }

  

3. 編寫通用增刪查改方法 insert、update、delete、Query(select)方法blog

   通用方法編寫思惟引導:當咱們在對數據庫進行操做時咱們發現無論怎麼對數據庫操做都少不了表名字段名那麼咱們能夠封裝函數用來存儲字段名和值

   如:insert into 表名 (字段名) values(值)

     update set 字段名=‘值’ from 表名 where 字段名=‘值’

     delete 表名 where 字段名=‘值’

     select 字段名 from 表名 where 字段名=‘值

     存儲字段名代碼(核心代碼):

 1 private static string GetNumericField<T>(T modelName)
 2         {
 3             try
 4             {
 5                 StringBuilder Start = new StringBuilder();
 6                 PropertyInfo[] props = modelName.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//抓取泛型類的字段屬性;
 7                 foreach (var item in props)
 8                 {
 9                  Start.Append(item.Name + ",");
10 
11                 }
12                 return Start.ToString().Substring(0, Start.Length - 1);
13             }
14             catch (Exception ex)
15             {
16                 throw ex;
17             }
18         }

    存儲值代碼(核心代碼):

 1 private static string GetValues<T>(T modelName)
 2         {
 3             try {
 4             StringBuilder End = new StringBuilder();
 5             PropertyInfo[] props = modelName.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//抓取泛型類的字段屬性;
 6             foreach (var item in props)
 7             {
 8                 if ((item.GetValue(modelName, null) + "").Equals("") || (item.GetValue(modelName, null) + "").Equals(null)) continue;
 9                 if (item.PropertyType.Name.StartsWith("String"))//判斷字段類型
10                 {
11                     End.Append(" and " + item.Name + " = '" + item.GetValue(modelName, null) + "'");
12                 }
13                 else if (item.PropertyType.Name.StartsWith("DateTime"))//判斷字段的屬性是否為時間類型//判斷字段類型
14                 {
15                     if (Convert.ToDateTime(item.GetValue(modelName, null)) == DateTime.MinValue)
16                     {
17                         continue;
18                     }
19                     else
20                     {
21                         End.Append(" and " + item.Name + " = '" + item.GetValue(modelName, null) + "'");
22                     }
23                 }
24                 else
25                 {
26                     End.Append(" and " + item.Name + " = " + item.GetValue(modelName, null) + "");
27                 }
28 
29             }
30             return End.ToString();
31             }
32             catch (Exception ex)
33             {
34                 throw ex;
35             }
36         }

下圖爲完整版的通用增刪改的代碼:

  1  /// <summary>
  2     /// 最低兼容.net 4.0(該ORM緊支持對單表操做)
  3     /// <para>開發時間:2019/5/27</para>
  4     /// <para>開發人員:會害羞的青蛙</para>
  5     /// </summary>
  6     public class BaseFunction
  7     {
  8         DBHelper DB = new DBHelper();
  9         /// <summary>
 10         /// 添加數據(數據錄入時必須將時間轉換為DateTime)
 11         /// </summary>
 12         /// <typeparam name="T"></typeparam>
 13         /// <param name="modelName"></param>
 14         /// <param name="dbName"></param>
 15         /// <returns></returns>
 16         public static int Save<T>(T modelName) {
 17             try {
 18             List<string> list = GetModelFeature(modelName);
 19             string dbName = list[0];
 20             StringBuilder SQL = new StringBuilder("insert into ");
 21             StringBuilder End = new StringBuilder();
 22             SQL.Append(dbName + " ("+ GetSaveStart(modelName)+") values ("+ GetSaveEnd(modelName) + ")");
 23             return DBHelper.ExecuteNonQuery(SQL.ToString().Trim().ToUpper());
 24             }
 25             catch (Exception ex)
 26             {
 27                 throw ex;
 28             }
 29         }
 30         /// <summary>
 31         /// 添加數據(數據錄入時必須將時間轉換為DateTime)
 32         /// <para>connectionString為自定義數據庫連接地址支持不一樣數據庫操做</para>
 33         /// </summary>
 34         /// <typeparam name="T"></typeparam>
 35         /// <param name="modelName"></param>
 36         /// <param name="dbName"></param>
 37         /// <returns></returns>
 38         public static int Save<T>(T modelName,string connectionString)
 39         {
 40             try
 41             {
 42                 List<string> list = GetModelFeature(modelName);
 43                 string dbName = list[0];
 44                 StringBuilder SQL = new StringBuilder("insert into ");
 45                 StringBuilder End = new StringBuilder();
 46                 SQL.Append(dbName + " (" + GetSaveStart(modelName) + ") values (" + GetSaveEnd(modelName) + ")");
 47                 return DBHelper.ExecuteNonQuery(SQL.ToString().Trim().ToUpper(), connectionString);
 48             }
 49             catch (Exception ex)
 50             {
 51                 throw ex;
 52             }
 53         }
 54         /// <summary>
 55         /// 根據主鍵ID修改數據(若沒有主鍵請在實體類定義一個主鍵,數據編輯時必須將時間轉換為DateTime)
 56         /// </summary>
 57         /// <typeparam name="T"></typeparam>
 58         /// <param name="modelName"></param>
 59         /// <param name="dbName"></param>
 60         /// <returns></returns>
 61         public static int Update<T>(T modelName)
 62         {
 63             try { 
 64             List<string> list = GetModelFeature(modelName);
 65             string dbName = list[0];//數據庫名
 66             StringBuilder SQL = new StringBuilder("update "+ dbName + " set ");
 67             StringBuilder End = new StringBuilder();
 68             SQL.Append(GetEditStart(modelName) +" where " + list[1]);
 69             return DBHelper.ExecuteNonQuery(SQL.ToString().Trim().ToUpper());
 70             }
 71             catch (Exception ex)
 72             {
 73                 throw ex;
 74             }
 75         }
 76         /// <summary>
 77         /// 根據主鍵ID修改數據(若沒有主鍵請在實體類定義一個主鍵,數據編輯時必須將時間轉換為DateTime)
 78         /// <para>connectionString為自定義數據庫連接地址支持不一樣數據庫操做</para>
 79         /// </summary>
 80         /// <typeparam name="T"></typeparam>
 81         /// <param name="modelName"></param>
 82         /// <param name="dbName"></param>
 83         /// <returns></returns>
 84         public static int Update<T>(T modelName, string connectionString)
 85         {
 86             try
 87             {
 88                 List<string> list = GetModelFeature(modelName);
 89                 string dbName = list[0];//數據庫名
 90                 StringBuilder SQL = new StringBuilder("update " + dbName + " set ");
 91                 StringBuilder End = new StringBuilder();
 92                 SQL.Append(GetEditStart(modelName) + " where " + list[1]);
 93                 return DBHelper.ExecuteNonQuery(SQL.ToString().Trim().ToUpper(), connectionString);
 94             }
 95             catch (Exception ex)
 96             {
 97                 throw ex;
 98             }
 99         }
100         /// <summary>
101         /// 根據主鍵ID刪除數據(若沒有主鍵請在實體類定義一個主鍵)
102         /// </summary>
103         /// <typeparam name="T"></typeparam>
104         /// <param name="modelName"></param>
105         /// <returns></returns>
106         public static int Delete<T>(T modelName)
107         {
108             try { 
109             List<string> list = GetModelFeature(modelName);
110             string dbName = list[0];//數據庫名
111             string primaryKey = string.Empty;
112             string primaryValue = string.Empty;
113             PropertyInfo[] props = modelName.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//抓取泛型類的字段屬性;
114             foreach (var item in props)
115             {
116                 object[] objAttrs = item.GetCustomAttributes(typeof(FieldAttribute), true);
117                 if (objAttrs.Length > 0)
118                 {
119                     FieldAttribute attr = objAttrs[0] as FieldAttribute;
120                     if (attr.PropertyKey == true)
121                     {
122                         primaryKey = item.Name;
123                         primaryValue = item.GetValue(modelName, null)+"";
124                     }
125                 }
126             }
127             if (primaryValue=="") { throw new Exception("主鍵不能爲空!"); }
128             StringBuilder SQL = new StringBuilder("delete " + dbName+ " where " + primaryKey+"='"+ primaryValue + "'");
129             return DBHelper.ExecuteNonQuery(SQL.ToString().Trim().ToUpper());
130             }
131             catch (Exception ex)
132             {
133                 throw ex;
134             }
135         }
136         /// <summary>
137         /// 根據主鍵ID刪除數據(若沒有主鍵請在實體類定義一個主鍵)
138         /// <para>connectionString為自定義數據庫連接地址支持不一樣數據庫操做</para>
139         /// </summary>
140         /// <typeparam name="T"></typeparam>
141         /// <param name="modelName"></param>
142         /// <returns></returns>
143         public static int Delete<T>(T modelName, string connectionString)
144         {
145             try
146             {
147                 List<string> list = GetModelFeature(modelName);
148                 string dbName = list[0];//數據庫名
149                 string primaryKey = string.Empty;
150                 string primaryValue = string.Empty;
151                 PropertyInfo[] props = modelName.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//抓取泛型類的字段屬性;
152                 foreach (var item in props)
153                 {
154                     object[] objAttrs = item.GetCustomAttributes(typeof(FieldAttribute), true);
155                     if (objAttrs.Length > 0)
156                     {
157                         FieldAttribute attr = objAttrs[0] as FieldAttribute;
158                         if (attr.PropertyKey == true)
159                         {
160                             primaryKey = item.Name;
161                             primaryValue = item.GetValue(modelName, null) + "";
162                         }
163                     }
164                 }
165                 if (primaryValue == "") { throw new Exception("主鍵不能爲空!"); }
166                 StringBuilder SQL = new StringBuilder("delete " + dbName + " where " + primaryKey + "='" + primaryValue + "'");
167                 return DBHelper.ExecuteNonQuery(SQL.ToString().Trim().ToUpper(), connectionString);
168             }
169             catch (Exception ex)
170             {
171                 throw ex;
172             }
173         }
174         /// <summary>
175         /// 查詢全部結果  modelName(實體類)
176         /// </summary>
177         /// <typeparam name="T"></typeparam>
178         /// <param name="modelName"></param>
179         /// <returns></returns>
180         public static DataTable QueryList<T>(T modelName) {
181             try { 
182             List<string> list = GetModelFeature(modelName);
183             string dbName = list[0];//數據庫名
184             StringBuilder Start = new StringBuilder();
185             Start.Append(GetQueryStart(modelName));
186             StringBuilder End = new StringBuilder();
187             End.Append(GetQueryEnd(modelName));
188             string SQL = "SELECT " + Start + " FROM " + dbName + " WHERE 1=1" + End;
189             return DBHelper.GetMultipleResults(SQL.ToString().Trim().ToUpper());
190             }
191             catch (Exception ex)
192             {
193                 throw ex;
194             }
195         }
196         /// <summary>
197         /// 查詢全部結果  modelName(實體類)
198         /// <para>connectionString為自定義數據庫連接地址支持不一樣數據庫操做</para>
199         /// </summary>
200         /// <typeparam name="T"></typeparam>
201         /// <param name="modelName"></param>
202         /// <returns></returns>
203         public static DataTable QueryList<T>(T modelName, string connectionString)
204         {
205             try
206             {
207                 List<string> list = GetModelFeature(modelName);
208                 string dbName = list[0];//數據庫名
209                 StringBuilder Start = new StringBuilder();
210                 Start.Append(GetQueryStart(modelName));
211                 StringBuilder End = new StringBuilder();
212                 End.Append(GetQueryEnd(modelName));
213                 string SQL = "SELECT " + Start + " FROM " + dbName + " WHERE 1=1" + End;
214                 return DBHelper.GetMultipleResults(SQL.ToString().Trim().ToUpper(), connectionString);
215             }
216             catch (Exception ex)
217             {
218                 throw ex;
219             }
220         }
221         /// <summary>
222         /// 查詢全部結果(包含分頁)modelName(實體類),page(當前頁),linmt(當前頁的總條數),count(結果集總條數)
223         /// </summary>
224         /// <typeparam name="T"></typeparam>
225         /// <param name="modelName"></param>
226         /// <param name="page"></param>
227         /// <param name="linmt"></param>
228         /// <returns></returns>
229         public static DataTable QueryList<T>(T modelName, int page, int linmt ,out int count)
230         {
231             try {
232                 List<string> list = GetModelFeature(modelName);
233                 string dbName = list[0];//數據庫名
234                 StringBuilder Start = new StringBuilder();
235                 Start.Append(GetQueryStart(modelName));
236                 StringBuilder StartToPagination = new StringBuilder();
237                 StartToPagination.Append(GetQueryStartToPagination(modelName));
238                 StringBuilder End = new StringBuilder();
239                 End.Append(GetQueryEnd(modelName));
240 
241                 count = Convert.ToInt32(DBHelper.GetMultipleResults("SELECT COUNT(*) AS COUNTNUM FROM " + dbName+" WHERE 1=1 "+ End).Rows[0]["COUNTNUM"]);
242                 string SQL = "SELECT " + StartToPagination + " FROM (SELECT ROWNUM AS NUM ," + Start + " FROM " + dbName + " WHERE 1=1" + End + " AND ROWNUM<=" + linmt + "*" + page + ") WHERE NUM>" + linmt + "*(" + page + "-1) ";
243                 return DBHelper.GetMultipleResults(SQL.ToString().Trim().ToUpper());
244             }
245             catch (Exception ex) {
246                 throw ex;
247             }
248         }
249         /// <summary>
250         /// 查詢全部結果(包含分頁)modelName(實體類),page(當前頁),linmt(當前頁的總條數),count(結果集總條數)
251         /// <para>connectionString為自定義數據庫連接地址支持不一樣數據庫操做</para>
252         /// </summary>
253         /// <typeparam name="T"></typeparam>
254         /// <param name="modelName"></param>
255         /// <param name="page"></param>
256         /// <param name="linmt"></param>
257         /// <returns></returns>
258         public static DataTable QueryList<T>(T modelName, string connectionString, int page, int linmt, out int count)
259         {
260             try
261             {
262                 List<string> list = GetModelFeature(modelName);
263                 string dbName = list[0];//數據庫名
264                 StringBuilder Start = new StringBuilder();
265                 Start.Append(GetQueryStart(modelName));
266                 StringBuilder StartToPagination = new StringBuilder();
267                 StartToPagination.Append(GetQueryStartToPagination(modelName));
268                 StringBuilder End = new StringBuilder();
269                 End.Append(GetQueryEnd(modelName));
270 
271                 count = Convert.ToInt32(DBHelper.GetMultipleResults("SELECT COUNT(*) AS COUNTNUM FROM " + dbName + " WHERE 1=1 " + End).Rows[0]["COUNTNUM"]);
272                 string SQL = "SELECT " + StartToPagination + " FROM (SELECT ROWNUM AS NUM ," + Start + " FROM " + dbName + " WHERE 1=1" + End + " AND ROWNUM<=" + linmt + "*" + page + ") WHERE NUM>" + linmt + "*(" + page + "-1) ";
273                 return DBHelper.GetMultipleResults(SQL.ToString().Trim().ToUpper(), connectionString);
274             }
275             catch (Exception ex)
276             {
277                 throw ex;
278             }
279         }
280         /// <summary>
281         /// 獲取實體類中的惟一id和對應的表名
282         /// </summary>
283         /// <typeparam name="T"></typeparam>
284         /// <param name="modelName"></param>
285         /// <returns></returns>
286         public static List<string> GetModelFeature<T>(T modelName) {
287             try {
288             List<string> list = new List<string>();
289             var info= modelName.GetType().GetCustomAttributes(typeof(TableAttribute), false);
290             foreach (var item in info)
291             {
292                 TableAttribute attr = item as TableAttribute;
293                 if (attr != null)
294                 {
295                     string tableName = attr.TableName;//表名只有獲取一次
296                     list.Add(tableName);
297                     break;
298                 }
299             }
300             PropertyInfo[] props = modelName.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//抓取泛型類的字段屬性;
301             foreach (var item in props)
302             {
303                 object[] objAttrs = item.GetCustomAttributes(typeof(FieldAttribute), true);
304                 if (objAttrs.Length > 0)
305                 {
306                     FieldAttribute attr = objAttrs[0] as FieldAttribute;
307                     if (attr.PropertyKey == true)
308                     {
309                         list.Add(item.Name+" = '"+ item.GetValue(modelName,null)+"'");//抓取篩選條件
310                     }
311                 }
312             }
313             return list;
314             }
315             catch (Exception ex)
316             {
317                 throw ex;
318             }
319         }
320         /// <summary>
321         /// 獲取起始字段
322         /// </summary>
323         /// <typeparam name="T"></typeparam>
324         /// <param name="modelName"></param>
325         /// <returns></returns>
326         private static string GetSaveStart<T>(T modelName) {
327             try {
328             StringBuilder Start = new StringBuilder();
329             PropertyInfo[]  props = modelName.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//抓取泛型類的字段屬性;
330             foreach ( PropertyInfo Info in props) {
331                 if ((Info.GetValue(modelName,null) + "").Equals("")|| (Info.GetValue(modelName, null) + "").Equals(null)) continue;
332                 if (Info.PropertyType.Name.StartsWith("DateTime"))
333                 {
334                     if (Convert.ToDateTime(Info.GetValue(modelName, null)) == DateTime.MinValue) continue;//判斷時間是否為最小時間 ;
335                 }
336                 Start.Append(Info.Name+",");
337             }
338             return Start.ToString().Substring(0, Start.Length - 1);
339             }
340             catch (Exception ex)
341             {
342                 throw ex;
343             }
344         }
345         /// <summary>
346         /// 獲取字段對應的結果
347         /// </summary>
348         /// <typeparam name="T"></typeparam>
349         /// <param name="modelName"></param>
350         /// <returns></returns>
351         private static string GetSaveEnd<T>(T modelName)
352         {
353             try { 
354             StringBuilder End = new StringBuilder();
355             PropertyInfo[] props = modelName.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//抓取泛型類的字段屬性;
356             foreach (PropertyInfo Info in props)
357             {
358                 if ((Info.GetValue(modelName, null) + "").Equals("") || (Info.GetValue(modelName, null) + "").Equals(null)) continue;
359                 
360                 if (Info.PropertyType.Name.StartsWith("String"))//判斷字段類型
361                 {
362                     End.Append("'" + Info.GetValue(modelName, null) + "',");
363                 }
364                 else if (Info.PropertyType.Name.StartsWith("DateTime"))//判斷字段類型
365                 {
366                     if (Convert.ToDateTime(Info.GetValue(modelName, null)) == DateTime.MinValue)//判斷時間是否為最小時間
367                     {
368                         continue;
369                     }
370                     else {
371                      End.Append("to_date('" + ((DateTime)(Info.GetValue(modelName, null))).ToString("yyyy-MM-dd HH:mm:ss") + "','YYYY-MM-DD HH24:MI:SS'),");
372                     }
373                 }
374                 else {
375                     End.Append(Info.GetValue(modelName, null) + ",");
376                 }
377             }
378             return End.ToString().Substring(0, End.Length - 1);
379             }
380             catch (Exception ex)
381             {
382                 throw ex;
383             }
384         }
385         /// <summary>
386         /// 從新定義分頁後的查詢字段並且格式化DateTime樣式
387         /// </summary>
388         /// <typeparam name="T"></typeparam>
389         /// <param name="modelName"></param>
390         /// <returns></returns>
391         private static string GetQueryStartToPagination<T>(T modelName) {
392             try { 
393             StringBuilder Start = new StringBuilder();
394             PropertyInfo[] props = modelName.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//抓取泛型類的字段屬性;
395             foreach (var item in props)
396             {
397                     if (item.PropertyType.Name.StartsWith("DateTime"))//判斷字段的屬性是否為時間類型
398                     {
399                         Start.Append("TO_CHAR(" + item.Name + ",'YYYY-MM-DD HH:mm:ss') AS " + item.Name + ",");//格式化時間
400                     }
401                     else {
402                         Start.Append(item.Name + ",");
403                     }
404                         
405             }
406             return Start.ToString().Substring(0, Start.Length - 1);
407             }
408             catch (Exception ex)
409             {
410                 throw ex;
411             }
412         }
413         private static string GetQueryStart<T>(T modelName)
414         {
415             try
416             {
417                 StringBuilder Start = new StringBuilder();
418                 PropertyInfo[] props = modelName.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//抓取泛型類的字段屬性;
419                 foreach (var item in props)
420                 {
421                  Start.Append(item.Name + ",");
422 
423                 }
424                 return Start.ToString().Substring(0, Start.Length - 1);
425             }
426             catch (Exception ex)
427             {
428                 throw ex;
429             }
430         }
431         private static string GetQueryEnd<T>(T modelName)
432         {
433             try {
434             StringBuilder End = new StringBuilder();
435             PropertyInfo[] props = modelName.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//抓取泛型類的字段屬性;
436             foreach (var item in props)
437             {
438                 if ((item.GetValue(modelName, null) + "").Equals("") || (item.GetValue(modelName, null) + "").Equals(null)) continue;
439                 if (item.PropertyType.Name.StartsWith("String"))//判斷字段類型
440                 {
441                     End.Append(" and " + item.Name + " = '" + item.GetValue(modelName, null) + "'");
442                 }
443                 else if (item.PropertyType.Name.StartsWith("DateTime"))//判斷字段的屬性是否為時間類型//判斷字段類型
444                 {
445                     if (Convert.ToDateTime(item.GetValue(modelName, null)) == DateTime.MinValue)
446                     {
447                         continue;
448                     }
449                     else
450                     {
451                         End.Append(" and " + item.Name + " = '" + item.GetValue(modelName, null) + "'");
452                     }
453                 }
454                 else
455                 {
456                     End.Append(" and " + item.Name + " = " + item.GetValue(modelName, null) + "");
457                 }
458 
459             }
460             return End.ToString();
461             }
462             catch (Exception ex)
463             {
464                 throw ex;
465             }
466         }
467         /// <summary>
468         /// 編輯起始內容
469         /// </summary>
470         /// <typeparam name="T"></typeparam>
471         /// <param name="modelName"></param>
472         /// <returns></returns>
473         private static string GetEditStart<T>(T modelName) {
474             try { 
475             StringBuilder Start = new StringBuilder();
476             PropertyInfo[] props = modelName.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);//抓取泛型類的字段屬性;
477             foreach (PropertyInfo Info in props)
478             {
479                 object[] objAttrs = Info.GetCustomAttributes(typeof(FieldAttribute), true);
480                 if (objAttrs.Length > 0)
481                 {
482                     FieldAttribute attr = objAttrs[0] as FieldAttribute;
483                     if (attr.PropertyKey == true) continue;//判斷自定義特性中的主鍵
484                 }
485                 if ((Info.GetValue(modelName, null) + "").Equals("") || (Info.GetValue(modelName, null) + "").Equals(null)) continue;
486 
487                 if (Info.PropertyType.Name.StartsWith("String"))//判斷字段類型
488                 {
489                     Start.Append(Info.Name + "='" + Info.GetValue(modelName, null) + "',");
490                 }
491                 else if (Info.PropertyType.Name.StartsWith("DateTime"))//判斷字段類型
492                 {
493                     if (Convert.ToDateTime(Info.GetValue(modelName, null)) == DateTime.MinValue)//判斷時間是否為最小時間
494                     {
495                         continue;
496                     }
497                     else
498                     {
499                         Start.Append(Info.Name+"=to_date('" + ((DateTime)(Info.GetValue(modelName, null))).ToString("yyyy-MM-dd HH:mm:ss") + "','YYYY-MM-DD HH24:MI:SS'),");
500                     }
501                 }
502                 else {
503                     Start.Append(Info.Name + "=" + Info.GetValue(modelName, null) + ",");
504                 }
505             }
506             return Start.ToString().Substring(0, Start.Length - 1);
507             }
508             catch (Exception ex)
509             {
510                 throw ex;
511             }
512         }
513     }

 

  警告:嚴禁以商用名義販賣此代碼

  代碼緊提供學習使用,若要轉載此教程備註原創者:會害羞的青蛙

相關文章
相關標籤/搜索