日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
如何對DataSet進(jìn)行強(qiáng)類型化

在項(xiàng)目中經(jīng)常需要用到DataSet來存放數(shù)據(jù),但是一直覺得從數(shù)據(jù)集中獲取數(shù)據(jù)使用是一件很難受的事情,特別是當(dāng)需要用到強(qiáng)類型數(shù)據(jù)的時(shí)候,就想到了動(dòng)手寫個(gè)方法來實(shí)現(xiàn)。

 
 
 
 
  1. ///     
  2.     /// 將數(shù)據(jù)集強(qiáng)類型化    
  3.     ///     
  4.     /// 轉(zhuǎn)換類型    
  5.     /// 數(shù)據(jù)源    
  6.     /// 需要轉(zhuǎn)換表的索引    
  7.     /// 泛型集合    
  8.     public static IList ToList(this DataSet dataSet, int tableIndex)    
  9.      {    
  10.          //確認(rèn)參數(shù)有效    
  11.          if (dataSet == null || dataSet.Tables.Count <= 0 || tableIndex < 0)    
  12.              return null;    
  13.          DataTable dt = dataSet.Tables[tableIndex];    
  14.          IList list = new List();    
  15.          for (int i = 0; i < dt.Rows.Count; i++)    
  16.          {    
  17.               //創(chuàng)建泛型對象    
  18.               T _t = Activator.CreateInstance();    
  19.               //獲取對象所有屬性    
  20.               PropertyInfo[] propertyInfo = _t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);    
  21.               for (int j = 0; j < dt.Columns.Count; j++)    
  22.               {    
  23.                   foreach (PropertyInfo info in propertyInfo)    
  24.                   {    
  25.                       //屬性名稱和列名相同時(shí)賦值    
  26.                       if (dt.Columns[j].ColumnName.ToUpper().Equals(info.Name.ToUpper()))    
  27.                       {    
  28.                           if (dt.Rows[i][j] != DBNull.Value)    
  29.                           {    
  30.                               info.SetValue(_t, dt.Rows[i][j].ConvertDataTo(info.PropertyType), null);    
  31.                           }    
  32.                           else   
  33.                           {    
  34.                               info.SetValue(_t, null, null);    
  35.                           }    
  36.                           break;    
  37.                       }    
  38.                   }    
  39.               }    
  40.               list.Add(_t);    
  41.          }    
  42.          return list;    
  43.      }  

在需要給屬性賦值的時(shí)候,為了避免數(shù)據(jù)集中的字段名與用戶定義的Model屬性名一致而類型不一致的問題,我又寫了個(gè)方法,用來把對象進(jìn)行類型轉(zhuǎn)換:

 
 
 
 
  1. public static object ConvertDataTo(this object obj,Type type)     
  2. {    
  3.     if (obj.GetType().Equals(type))    
  4.     {    
  5.         return obj;    
  6.     }    
  7.     else   
  8.     {    
  9.         try   
  10.         {    
  11.             if (type == typeof(string)) { return obj.ToString(); }    
  12.             MethodInfo parseMethod = null;    
  13.             foreach (MethodInfo mi in type.GetMethods(BindingFlags.Static | BindingFlags.Public))    
  14.             {    
  15.                 if (mi.Name == "Parse" && mi.GetParameters().Length == 1)    
  16.                 { parseMethod = mi; break; }    
  17.             }    
  18.             if (parseMethod == null)    
  19.             {    
  20.                 return null;    
  21.             }    
  22.             return parseMethod.Invoke(null, new object[] { obj });     
  23.         }    
  24.         catch    
  25.         {    
  26.             return null;    
  27.             throw;    
  28.         }    
  29.     }    
  30. }  

其實(shí)這么寫是比較偷懶的寫法,用了這么多反射, 于是想到做一下性能測試,我建的MVC項(xiàng)目,看一下測試結(jié)果:

 
 
 
 
  1. public ActionResult Index()    
  2.      {    
  3.          DataSet ds = new DataSet();    
  4.          DataTable dt = new DataTable();    
  5.          dt.Columns.Add("resourcename1", typeof(string));    
  6.          dt.Columns.Add("resourcename2", typeof(string));    
  7.          dt.Columns.Add("resourcename3", typeof(string));    
  8.          dt.Columns.Add("resourcename4", typeof(string));    
  9.          dt.Columns.Add("resourcename5", typeof(string));    
  10.          dt.Columns.Add("fitsex1", typeof(int));    
  11.          dt.Columns.Add("fitsex2", typeof(int));    
  12.          dt.Columns.Add("fitsex3", typeof(int));    
  13.          dt.Columns.Add("fitsex4", typeof(int));    
  14.          dt.Columns.Add("fitsex5", typeof(int));    
  15.          for (int i = 0; i < 5000; i++)    
  16.          {    
  17.               DataRow row = dt.NewRow();    
  18.               row[0] = "王虎" + i.ToString();    
  19.               row[1] = "王虎" + i.ToString();    
  20.               row[2] = "王虎" + i.ToString();    
  21.               row[3] = "王虎" + i.ToString();    
  22.               row[4] = "王虎" + i.ToString();    
  23.               row[5] = i;    
  24.               row[6] = i;    
  25.               row[7] = i;    
  26.               row[8] = i;    
  27.               row[9] = i;    
  28.               dt.Rows.Add(row);    
  29.           }    
  30.           ds.Tables.Add(dt);    
  31.           var watch = new Stopwatch();    
  32.           watch.Start();    
  33.           var ModelList = ds.ToList(0);    
  34.           watch.Stop();    
  35.           ViewData["Message"] = string.Format("ModelList.count={0},Elapsed Milliseconds:{1}", ModelList.Count.ToString(),watch.ElapsedMilliseconds.ToString());    
  36.           return View();    
  37.      }  

我使用的類定義如下:

 
 
 
 
  1. ///     
  2.   /// 實(shí)體類Resource 。(屬性說明自動(dòng)提取數(shù)據(jù)庫字段的描述信息)    
  3.   ///     
  4.   [Serializable]    
  5.   public class Model_Resource    
  6.   {    
  7.       public Model_Resource()    
  8.       { }    
  9.       #region Model    
  10.       ///     
  11.       /// 資源標(biāo)準(zhǔn)名稱    
  12.       ///     
  13.       public string ResourceName1    
  14.       {    
  15.           get;    
  16.           set;    
  17.       }    
  18.       ///     
  19.       /// 資源標(biāo)準(zhǔn)名稱    
  20.       ///     
  21.       public string ResourceName2    
  22.       {    
  23.           get;    
  24.           set;    
  25.       }    
  26.       ///     
  27.       /// 資源標(biāo)準(zhǔn)名稱    
  28.       ///     
  29.       public string ResourceName3    
  30.       {    
  31.           get;    
  32.           set;    
  33.       }    
  34.       ///     
  35.       /// 資源標(biāo)準(zhǔn)名稱    
  36.       ///     
  37.       public string ResourceName4    
  38.       {    
  39.           get;    
  40.           set;    
  41.       }    
  42.       ///     
  43.       /// 資源標(biāo)準(zhǔn)名稱    
  44.       ///     
  45.       public string ResourceName5    
  46.       {    
  47.           get;    
  48.           set;    
  49.       }    
  50.       ///     
  51.       /// 適合的性別 1 男 2 女 3 均可    
  52.       ///     
  53.       public string FitSex1    
  54.       {    
  55.           get;    
  56.           set;    
  57.       }    
  58.       ///     
  59.       /// 適合的性別 1 男 2 女 3 均可    
  60.       ///     
  61.       public string FitSex2    
  62.       {    
  63.           get;    
  64.           set;    
  65.       }    
  66.       ///     
  67.       /// 適合的性別 1 男 2 女 3 均可    
  68.       ///     
  69.       public string FitSex3    
  70.       {    
  71.           get;    
  72.           set;    
  73.       }    
  74.       ///     
  75.       /// 適合的性別 1 男 2 女 3 均可    
  76.       ///     
  77.       public string FitSex4    
  78.       {    
  79.           get;    
  80.           set;    
  81.       }    
  82.       ///     
  83.       /// 適合的性別 1 男 2 女 3 均可    
  84.       ///     
  85.       public string FitSex5    
  86.       {    
  87.           get;    
  88.           set;    
  89.       }    
  90.       #endregion Model    
  91.   }  

看一下測試結(jié)果:

注:

性能上還可以通過緩存等機(jī)制優(yōu)化一下,不過這方面已經(jīng)有一些大牛做過了,以后有時(shí)間可以加進(jìn)去。

原文鏈接:http://www.cnblogs.com/wbpmrck/archive/2011/04/12/2013730.html

【編輯推薦】

  1. 一步一步設(shè)計(jì)你的數(shù)據(jù)庫1
  2. 為自己做一個(gè)簡單記賬簿
  3. 曬曬我的通用數(shù)據(jù)訪問層
  4. 幾步走,教你創(chuàng)建簡單訪問數(shù)據(jù)庫方法
  5. 微軟研究人員:NoSQL需要標(biāo)準(zhǔn)化

網(wǎng)站標(biāo)題:如何對DataSet進(jìn)行強(qiáng)類型化
轉(zhuǎn)載源于:http://m.5511xx.com/article/dhioeoc.html