基于Typed DataSet数据访问的实现

VS Studio可以帮助我们通过简单的拖拽生成Typed DataSet。同时自动生成了DataAdapter,更加简化了数据的访问操作。
下面是我基于VS 2005编写的一个类,为基于Typed DataSet的数据访问提供了比较简单的方法。
基类使用泛型,反射等方法实现了数据访问的封装。在定义实体类时,只需要继承该基类就可以实现数据的读取、保存和删除操作。

  1 None.gif using  System;
  2 None.gif using  System.Collections.Generic;
  3 None.gif using  System.Text;
  4 None.gif using  System.Reflection;
  5 None.gif using  System.Data;
  6 None.gif
  7 None.gif namespace  SimpleCode.Core.Model
  8 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  9InBlock.gif    public class ModelBase<TAdapter, TDataTable, TRow> : SimpleCode.Core.Interface.IModel
 10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 11InBlock.gif
 12ContractedSubBlock.gifExpandedSubBlockStart.gif        Variable define#region Variable define
 13InBlock.gif
 14InBlock.gif        private object m_Adapter;
 15InBlock.gif        private Type m_TypeAdpt;
 16InBlock.gif
 17InBlock.gif
 18InBlock.gif        private object m_Table;
 19InBlock.gif        private Type m_TypeTable;
 20InBlock.gif
 21InBlock.gif
 22InBlock.gif        private object m_Row;
 23InBlock.gif        private Type m_TypeRow;
 24InBlock.gif
 25InBlock.gif
 26InBlock.gif        private TRow m_CurrentItem;
 27InBlock.gif
 28InBlock.gif
 29InBlock.gif        private TDataTable m_Items;
 30InBlock.gif
 31InBlock.gif        private string m_ItemId;
 32InBlock.gif
 33InBlock.gif        private string m_TableName;
 34InBlock.gif
 35ExpandedSubBlockEnd.gif        #endregion

 36InBlock.gif
 37ContractedSubBlock.gifExpandedSubBlockStart.gif        Property define#region Property define
 38InBlock.gif
 39ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 40InBlock.gif        /// 适配器
 41ExpandedSubBlockEnd.gif        /// </summary>

 42InBlock.gif        public TAdapter Apapter
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 44ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn (TAdapter)m_Adapter; }
 45ExpandedSubBlockEnd.gif        }

 46InBlock.gif
 47ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 48InBlock.gif        /// 数据表
 49ExpandedSubBlockEnd.gif        /// </summary>

 50InBlock.gif        public TDataTable Table
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 52ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn (TDataTable)m_Table; }
 53ExpandedSubBlockEnd.gif        }

 54InBlock.gif
 55ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 56InBlock.gif        /// 数据表中的所有数据
 57ExpandedSubBlockEnd.gif        /// </summary>

 58InBlock.gif        public TDataTable Items
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 60InBlock.gif            get
 61ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 62InBlock.gif                if (m_Items == null)
 63ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 64InBlock.gif                    this.m_Items = GetItems();
 65ExpandedSubBlockEnd.gif                }

 66InBlock.gif                return m_Items;
 67ExpandedSubBlockEnd.gif            }

 68ExpandedSubBlockEnd.gif        }

 69InBlock.gif
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 71InBlock.gif        /// 当前的数据项
 72ExpandedSubBlockEnd.gif        /// </summary>

 73InBlock.gif        public TRow CurrentItem
 74ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 75ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifif (this.m_CurrentItem != nullreturn this.m_CurrentItem; else return this.m_CurrentItem = NewRow(); }
 76ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gifthis.m_CurrentItem = value; }
 77ExpandedSubBlockEnd.gif        }

 78InBlock.gif
 79ExpandedSubBlockEnd.gif        #endregion

 80InBlock.gif
 81ContractedSubBlock.gifExpandedSubBlockStart.gif        Constructor#region Constructor
 82InBlock.gif
 83InBlock.gif        public ModelBase()
 84ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 85InBlock.gif            Initialize();
 86ExpandedSubBlockEnd.gif        }

 87InBlock.gif
 88InBlock.gif        public ModelBase(string id)
 89InBlock.gif            : this()
 90ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 91InBlock.gif            this.m_ItemId = id;
 92InBlock.gif            m_CurrentItem = GetItemById(this.m_ItemId);
 93ExpandedSubBlockEnd.gif        }

 94InBlock.gif
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 96InBlock.gif        /// 初始化
 97ExpandedSubBlockEnd.gif        /// </summary>

 98InBlock.gif        private void Initialize()
 99ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
100InBlock.gif            //初始化适配器
101InBlock.gif            m_TypeAdpt = typeof(TAdapter);
102InBlock.gif            m_Adapter = Activator.CreateInstance<TAdapter>();
103InBlock.gif
104InBlock.gif            //初始化数据表
105InBlock.gif            m_TypeTable = typeof(TDataTable);
106InBlock.gif            m_Table = Activator.CreateInstance<TDataTable>();
107InBlock.gif
108InBlock.gif            //初始化数据行
109InBlock.gif            m_TypeRow = typeof(TRow);
110InBlock.gif
111InBlock.gif            this.m_TableName = ((DataTable)this.m_Table).TableName;
112ExpandedSubBlockEnd.gif        }

113InBlock.gif
114ExpandedSubBlockEnd.gif        #endregion

115InBlock.gif
116ContractedSubBlock.gifExpandedSubBlockStart.gif        Private Method#region Private Method
117InBlock.gif
118ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
119InBlock.gif        /// 获取所有数据
120InBlock.gif        /// </summary>
121ExpandedSubBlockEnd.gif        /// <returns></returns>

122InBlock.gif        private TDataTable GetItems()
123ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
124InBlock.gif            MethodInfo method = this.m_TypeAdpt.GetMethod("GetData");
125InBlock.gif            return (TDataTable)method.Invoke(this.m_Adapter, null);
126ExpandedSubBlockEnd.gif        }

127InBlock.gif
128ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
129InBlock.gif        /// 删除数据
130InBlock.gif        /// </summary>
131ExpandedSubBlockEnd.gif        /// <param name="rows"></param>

132InBlock.gif        private void DeleteRow(TRow[] rows)
133ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
134InBlock.gif            foreach (TRow row in rows)
135ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
136InBlock.gif                DataRow dr = row as DataRow;
137InBlock.gif                dr.Delete();
138ExpandedSubBlockEnd.gif            }

139ExpandedSubBlockStart.gifContractedSubBlock.gif            MethodInfo methodUpdate = this.m_TypeAdpt.GetMethod("Update"new Type[] dot.giftypeof(System.Data.DataRow[]) });
140ExpandedSubBlockStart.gifContractedSubBlock.gif            object[] parameters = dot.gif{ rows };
141InBlock.gif            methodUpdate.Invoke(this.m_Adapter, parameters);
142ExpandedSubBlockEnd.gif        }

143InBlock.gif
144ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
145InBlock.gif        /// 删除当前数据
146ExpandedSubBlockEnd.gif        /// </summary>

147InBlock.gif        private void DeleteRow()
148ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
149InBlock.gif            DataRow dr = this.m_CurrentItem as DataRow;
150InBlock.gif            dr.Delete();
151InBlock.gif
152ExpandedSubBlockStart.gifContractedSubBlock.gif            MethodInfo methodUpdate = this.m_TypeAdpt.GetMethod("Update"new Type[] dot.giftypeof(System.Data.DataRow) });
153ExpandedSubBlockStart.gifContractedSubBlock.gif            object[] parameters = dot.gifthis.m_CurrentItem };
154InBlock.gif            methodUpdate.Invoke(this.m_Adapter, parameters);
155ExpandedSubBlockEnd.gif        }

156InBlock.gif
157ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
158InBlock.gif        /// 新建数据项
159InBlock.gif        /// </summary>
160ExpandedSubBlockEnd.gif        /// <returns></returns>

161InBlock.gif        private TRow NewRow()
162ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
163InBlock.gif            MethodInfo method = this.m_TypeTable.GetMethod(string.Format("New{0}Row"this.m_TableName));
164InBlock.gif            return (TRow)method.Invoke(this.m_Table, null);
165ExpandedSubBlockEnd.gif        }

166InBlock.gif
167ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
168InBlock.gif        /// 保存当前数据
169InBlock.gif        /// </summary>
170ExpandedSubBlockEnd.gif        /// <returns></returns>

171InBlock.gif        private void SaveItem()
172ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
173InBlock.gif            DataRow dr = this.m_CurrentItem as DataRow;
174InBlock.gif
175InBlock.gif            //如果DataRowState为Detached,则为新增数据
176InBlock.gif            if (dr.RowState == DataRowState.Detached)
177ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
178InBlock.gif                ((DataTable)this.m_Table).Rows.Add(dr);
179InBlock.gif
180ExpandedSubBlockStart.gifContractedSubBlock.gif                MethodInfo methodUpdate = this.m_TypeAdpt.GetMethod("Update"new Type[] dot.giftypeof(TDataTable) });
181ExpandedSubBlockStart.gifContractedSubBlock.gif                methodUpdate.Invoke(this.m_Adapter, new object[] dot.gif{ (TDataTable)this.m_Table });
182ExpandedSubBlockEnd.gif            }

183InBlock.gif            else
184ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
185ExpandedSubBlockStart.gifContractedSubBlock.gif                MethodInfo methodUpdate = this.m_TypeAdpt.GetMethod("Update"new Type[] dot.giftypeof(DataRow) });
186ExpandedSubBlockStart.gifContractedSubBlock.gif                methodUpdate.Invoke(this.m_Adapter, new object[] dot.gifthis.m_CurrentItem });
187ExpandedSubBlockEnd.gif            }

188ExpandedSubBlockEnd.gif        }

189InBlock.gif
190ExpandedSubBlockEnd.gif        #endregion

191InBlock.gif
192ContractedSubBlock.gifExpandedSubBlockStart.gif        Public Method#region Public Method
193InBlock.gif
194ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
195InBlock.gif        /// 获取复合条件的数据
196InBlock.gif        /// </summary>
197InBlock.gif        /// <param name="filter">筛选条件</param>
198ExpandedSubBlockEnd.gif        /// <returns></returns>

199InBlock.gif        public TRow[] GetItems(string filter)
200ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
201InBlock.gif            DataTable dt = this.Items as DataTable;
202InBlock.gif            TRow[] rows = dt.Select(filter) as TRow[];
203InBlock.gif
204InBlock.gif            if (rows.Length > 0)
205ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
206InBlock.gif                return rows;
207ExpandedSubBlockEnd.gif            }

208InBlock.gif            return default(TRow[]);
209ExpandedSubBlockEnd.gif        }

210InBlock.gif
211ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
212InBlock.gif        /// 获取制定Id的数据行
213InBlock.gif        /// </summary>
214InBlock.gif        /// <param name="id"></param>
215ExpandedSubBlockEnd.gif        /// <returns></returns>

216InBlock.gif        public TRow GetItemById(string id)
217ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
218InBlock.gif
219InBlock.gif            DataTable dt = this.Items as DataTable;
220InBlock.gif            //获取表的主键
221InBlock.gif            DataColumn[] cols = dt.PrimaryKey;
222InBlock.gif
223InBlock.gif            //ToDo:考虑多主键的情况
224InBlock.gif
225InBlock.gif            string filter = string.Format("{0} = '{1}'", cols[0].ColumnName, id);
226InBlock.gif
227InBlock.gif            TRow[] rows = dt.Select(filter) as TRow[];
228InBlock.gif            if (rows.Length > 0)
229ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
230InBlock.gif                return rows[0];
231ExpandedSubBlockEnd.gif            }

232InBlock.gif            return default(TRow);
233ExpandedSubBlockEnd.gif        }

234InBlock.gif
235ContractedSubBlock.gifExpandedSubBlockStart.gif        IModel Members#region IModel Members
236InBlock.gif
237InBlock.gif        public bool Delete()
238ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
239InBlock.gif            try
240ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
241InBlock.gif                //如果数据有主外键关系,则删除不成功。
242InBlock.gif                DeleteRow();
243InBlock.gif                return true;
244ExpandedSubBlockEnd.gif            }

245InBlock.gif            catch (Exception ex)
246ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
247InBlock.gif                throw new Exception(ex.StackTrace);
248ExpandedSubBlockEnd.gif            }

249ExpandedSubBlockEnd.gif        }

250InBlock.gif
251InBlock.gif        public bool Save()
252ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
253InBlock.gif            try
254ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
255InBlock.gif                SaveItem();
256InBlock.gif                return true;
257ExpandedSubBlockEnd.gif            }

258InBlock.gif            catch (Exception ex)
259ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
260InBlock.gif                throw new Exception(ex.StackTrace);
261ExpandedSubBlockEnd.gif            }

262ExpandedSubBlockEnd.gif        }

263InBlock.gif
264ExpandedSubBlockEnd.gif        #endregion

265InBlock.gif
266ExpandedSubBlockEnd.gif        #endregion

267InBlock.gif
268ExpandedSubBlockEnd.gif    }

269ExpandedBlockEnd.gif}

270 None.gif
在数据层中创建Typed DataSet

在业务层中定义Customer类
 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.Text;
 4 None.gif using  SimpleCode.Demo.DataModel;
 5 None.gif using  SimpleCode.Demo.DataModel.DemoDataTableAdapters;
 6 None.gif using  SimpleCode.Core.Model;
 7 None.gif
 8 None.gif namespace  SimpleCode.Demo.BL
 9 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
10InBlock.gif    public class Customer : ModelBase<CustomerTableAdapter,DemoData.CustomerDataTable,DemoData.CustomerRow>
11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
12ExpandedSubBlockStart.gifContractedSubBlock.gif        public Customer()dot.gif{ }
13InBlock.gif
14ExpandedSubBlockStart.gifContractedSubBlock.gif        public Customer(string id):base(id)dot.gif{}        
15ExpandedSubBlockEnd.gif    }

16ExpandedBlockEnd.gif}

在应用层中可以通过以下方式访问
 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.Text;
 4 None.gif using  SimpleCode.Demo.BL;
 5 None.gif
 6 None.gif namespace  SimpleCode.Demo.Test
 7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 8InBlock.gif    class Program
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10InBlock.gif        static void Main(string[] args)
11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
12InBlock.gif            Customer myCustomer = new Customer();
13InBlock.gif            myCustomer.CurrentItem.CustomerID = "00125";
14InBlock.gif            myCustomer.CurrentItem.Name = "Jason Lee";
15InBlock.gif            myCustomer.CurrentItem.Address = "Shanghai";
16InBlock.gif            //Add Customer Informations
17InBlock.gif            myCustomer.Save();
18InBlock.gif
19InBlock.gif            Customer myCustomer2 = new Customer("00125");
20InBlock.gif
21InBlock.gif            Console.WriteLine("Customer Name is " + myCustomer2.CurrentItem.Name);
22ExpandedSubBlockEnd.gif        }

23ExpandedSubBlockEnd.gif    }

24ExpandedBlockEnd.gif}

25 None.gif

转载于:https://www.cnblogs.com/wenjielee/archive/2007/05/21/753960.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值