新聞中心
這里有您想知道的互聯(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í)現(xiàn)。

- ///
- /// 將數(shù)據(jù)集強(qiáng)類型化
- ///
- ///
轉(zhuǎn)換類型 - /// 數(shù)據(jù)源
- /// 需要轉(zhuǎn)換表的索引
- ///
泛型集合 - public static IList
ToList (this DataSet dataSet, int tableIndex) - {
- //確認(rèn)參數(shù)有效
- if (dataSet == null || dataSet.Tables.Count <= 0 || tableIndex < 0)
- return null;
- DataTable dt = dataSet.Tables[tableIndex];
- IList
list = new List (); - for (int i = 0; i < dt.Rows.Count; i++)
- {
- //創(chuàng)建泛型對象
- T _t = Activator.CreateInstance
(); - //獲取對象所有屬性
- PropertyInfo[] propertyInfo = _t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
- for (int j = 0; j < dt.Columns.Count; j++)
- {
- foreach (PropertyInfo info in propertyInfo)
- {
- //屬性名稱和列名相同時賦值
- if (dt.Columns[j].ColumnName.ToUpper().Equals(info.Name.ToUpper()))
- {
- if (dt.Rows[i][j] != DBNull.Value)
- {
- info.SetValue(_t, dt.Rows[i][j].ConvertDataTo(info.PropertyType), null);
- }
- else
- {
- info.SetValue(_t, null, null);
- }
- break;
- }
- }
- }
- list.Add(_t);
- }
- return list;
- }
在需要給屬性賦值的時候,為了避免數(shù)據(jù)集中的字段名與用戶定義的Model屬性名一致而類型不一致的問題,我又寫了個方法,用來把對象進(jìn)行類型轉(zhuǎn)換:
- public static object ConvertDataTo(this object obj,Type type)
- {
- if (obj.GetType().Equals(type))
- {
- return obj;
- }
- else
- {
- try
- {
- if (type == typeof(string)) { return obj.ToString(); }
- MethodInfo parseMethod = null;
- foreach (MethodInfo mi in type.GetMethods(BindingFlags.Static | BindingFlags.Public))
- {
- if (mi.Name == "Parse" && mi.GetParameters().Length == 1)
- { parseMethod = mi; break; }
- }
- if (parseMethod == null)
- {
- return null;
- }
- return parseMethod.Invoke(null, new object[] { obj });
- }
- catch
- {
- return null;
- throw;
- }
- }
- }
其實(shí)這么寫是比較偷懶的寫法,用了這么多反射, 于是想到做一下性能測試,我建的MVC項(xiàng)目,看一下測試結(jié)果:
- public ActionResult Index()
- {
- DataSet ds = new DataSet();
- DataTable dt = new DataTable();
- dt.Columns.Add("resourcename1", typeof(string));
- dt.Columns.Add("resourcename2", typeof(string));
- dt.Columns.Add("resourcename3", typeof(string));
- dt.Columns.Add("resourcename4", typeof(string));
- dt.Columns.Add("resourcename5", typeof(string));
- dt.Columns.Add("fitsex1", typeof(int));
- dt.Columns.Add("fitsex2", typeof(int));
- dt.Columns.Add("fitsex3", typeof(int));
- dt.Columns.Add("fitsex4", typeof(int));
- dt.Columns.Add("fitsex5", typeof(int));
- for (int i = 0; i < 5000; i++)
- {
- DataRow row = dt.NewRow();
- row[0] = "王虎" + i.ToString();
- row[1] = "王虎" + i.ToString();
- row[2] = "王虎" + i.ToString();
- row[3] = "王虎" + i.ToString();
- row[4] = "王虎" + i.ToString();
- row[5] = i;
- row[6] = i;
- row[7] = i;
- row[8] = i;
- row[9] = i;
- dt.Rows.Add(row);
- }
- ds.Tables.Add(dt);
- var watch = new Stopwatch();
- watch.Start();
- var ModelList = ds.ToList
(0); - watch.Stop();
- ViewData["Message"] = string.Format("ModelList.count={0},Elapsed Milliseconds:{1}", ModelList.Count.ToString(),watch.ElapsedMilliseconds.ToString());
- return View();
- }
我使用的類定義如下:
- ///
- /// 實(shí)體類Resource 。(屬性說明自動提取數(shù)據(jù)庫字段的描述信息)
- ///
- [Serializable]
- public class Model_Resource
- {
- public Model_Resource()
- { }
- #region Model
- ///
- /// 資源標(biāo)準(zhǔn)名稱
- ///
- public string ResourceName1
- {
- get;
- set;
- }
- ///
- /// 資源標(biāo)準(zhǔn)名稱
- ///
- public string ResourceName2
- {
- get;
- set;
- }
- ///
- /// 資源標(biāo)準(zhǔn)名稱
- ///
- public string ResourceName3
- {
- get;
- set;
- }
- ///
- /// 資源標(biāo)準(zhǔn)名稱
- ///
- public string ResourceName4
- {
- get;
- set;
- }
- ///
- /// 資源標(biāo)準(zhǔn)名稱
- ///
- public string ResourceName5
- {
- get;
- set;
- }
- ///
- /// 適合的性別 1 男 2 女 3 均可
- ///
- public string FitSex1
- {
- get;
- set;
- }
- ///
- /// 適合的性別 1 男 2 女 3 均可
- ///
- public string FitSex2
- {
- get;
- set;
- }
- ///
- /// 適合的性別 1 男 2 女 3 均可
- ///
- public string FitSex3
- {
- get;
- set;
- }
- ///
- /// 適合的性別 1 男 2 女 3 均可
- ///
- public string FitSex4
- {
- get;
- set;
- }
- ///
- /// 適合的性別 1 男 2 女 3 均可
- ///
- public string FitSex5
- {
- get;
- set;
- }
- #endregion Model
- }
看一下測試結(jié)果:
注:
性能上還可以通過緩存等機(jī)制優(yōu)化一下,不過這方面已經(jīng)有一些大牛做過了,以后有時間可以加進(jìn)去。
原文鏈接:http://www.cnblogs.com/wbpmrck/archive/2011/04/12/2013730.html
【編輯推薦】
- 一步一步設(shè)計你的數(shù)據(jù)庫1
- 為自己做一個簡單記賬簿
- 曬曬我的通用數(shù)據(jù)訪問層
- 幾步走,教你創(chuàng)建簡單訪問數(shù)據(jù)庫方法
- 微軟研究人員:NoSQL需要標(biāo)準(zhǔn)化
分享標(biāo)題:如何對DataSet進(jìn)行強(qiáng)類型化
當(dāng)前路徑:http://m.fisionsoft.com.cn/article/dhioeoc.html


咨詢
建站咨詢
