[代码]Object/DataSet Relational Mapping(对象/数据集关系映射)补

  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Collections;
  5using System.Web;
  6using System.Web.Security;
  7using System.Web.UI;
  8using System.Web.UI.WebControls;
  9using System.Web.UI.WebControls.WebParts;
 10using System.Web.UI.HtmlControls;
 11
 12using IBatisNet.DataMapper;
 13using System.Reflection;
 14
 15/// <summary>
 16/// ODRM为结合ORM与DataSet,并自动根据O和DataSet生成对象,以便业务层处理
 17/// </summary>

 18public partial class ODRM_test : PageBase
 19{
 20    protected void Page_Load(object sender, EventArgs e)
 21    {
 22        if (!IsPostBack)
 23        {
 24            DataSet set11 = Mapper.Instance().QueryForDataSet("SelectXTM_UserByKey_Test",UIhashtable);
 25            DataTable table1 = ConvertDataTable(set11, "");
 26            //这里为自己定义的序列化类
 27            cXTM_User[] objModel = new cXTM_User[table1.Rows.Count];
 28            //DataTable转化为序列化类数组
 29            for (int y = 0; y < table1.Rows.Count; y++)
 30            {
 31                objModel[y] = new cXTM_User();
 32                DataTableConvertObject(table1.Rows[y], objModel[y]); 
 33            }

 34            //以DataSet模式绑定
 35            ExDataGrid1.DataSource = table1;
 36            //以序列化对象模式绑定
 37            //ExDataGrid1.DataSource = objModel;
 38            ExDataGrid1.DataBind();
 39        }

 40    }

 41
 42    protected void ExDataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
 43    {
 44        /*
 45         * 该部分应用范围
 46         * 查询一条数据的修改,可以用objModel.UserName
 47         * 而不必再使用DataTable[0].Rows[0]["UserName"]的模式
 48         * 提高面向对象的程度,并减少业务流程部分编码
 49         */

 50
 51        if (e.Item.ItemIndex != 1)
 52        {
 53            cXTM_User objModel = new cXTM_User();
 54            
 55            //如果为DataSet填充的DataGrid
 56            if (e.Item.DataItem.GetType().FullName == "System.Data.DataRowView")
 57            {
 58                DataTableConvertObject((DataRow)((DataRowView)e.Item.DataItem).Row, objModel);      
 59            }

 60            //否则认为为序列化对象填充
 61            else 
 62            {
 63                objModel = (cXTM_User)e.Item.DataItem; 
 64                   
 65            }

 66        }

 67    }

 68
 69    #region 指定对象函数
 70    /// <summary>
 71    /// 数据集中一行DataRow转换为指定对象,并填充数据
 72    /// </summary>
 73    /// <param name="row">数据集中一行</param>
 74    /// <param name="objModel">指定对象</param>

 75    private void DataTableConvertObject(DataRow row, cXTM_User objModel)
 76    {
 77        Hashtable hTable = new Hashtable();
 78        hTable = DataRowConvertHashtable(row);
 79        Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
 80
 81        for (int j = 0; j < objModel.Propertylist.Length; j++)
 82        {
 83            PropertyInfo propertyinfo = entitytype.GetProperty(objModel.Propertylist[j]);
 84            propertyinfo.SetValue(objModel, hTable[objModel.Propertylist[j]], null);
 85        }

 86    }

 87
 88    /// <summary>
 89    /// 对象转换为哈希表
 90    /// </summary>
 91    /// <param name="objModel">有数据的对象</param>
 92    /// <returns>填充数据后的哈希表</returns>

 93    public Hashtable ObjectConvertHashtable(cXTM_User objModel)
 94    {
 95        Hashtable hTable = new Hashtable();
 96        Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
 97        for (int j = 0; j < objModel.Propertylist.Length; j++)
 98        {
 99            PropertyInfo propertyinfo = entitytype.GetProperty(objModel.Propertylist[j]);
100            hTable.Add(objModel.Propertylist[j], propertyinfo.GetValue(objModel, null));
101        }

102        return hTable;
103    }

104
105    /// <summary>
106    /// 对象转换为DataTable,并有单行DataRow
107    /// </summary>
108    /// <param name="objModel">有数据的对象</param>
109    /// <returns></returns>

110    public DataTable ObjectConvertDataTableWidthRow(cXTM_User objModel)
111    {
112        return ObjectConvertDataTableWidthRow(objModel, "");
113    }

114
115    /// <summary>
116    /// 对象转换为DataTable,并有单行DataRow
117    /// </summary>
118    /// <param name="objModel">有数据的对象</param>
119    /// <returns></returns>

120    public DataTable ObjectConvertDataTableWidthRow(cXTM_User objModel, string DataMapper)
121    {
122        Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
123        DataTable dt = new DataTable();
124        if (DataMapper != "")
125        {
126            dt = new DataTable(DataMapper);
127        }

128        dt.Columns.Clear();
129        for (int j = 0; j < objModel.Propertylist.Length; j++)
130        {
131            PropertyInfo propertyinfo = entitytype.GetProperty(objModel.Propertylist[j]);
132            dt.Columns.Add(new DataColumn(objModel.Propertylist[j], propertyinfo.GetType()));
133        }

134        DataRow row = dt.NewRow();
135        for (int j = 0; j < objModel.Propertylist.Length; j++)
136        {
137            PropertyInfo propertyinfo = entitytype.GetProperty(objModel.Propertylist[j]);
138            row[objModel.Propertylist[j]] = propertyinfo.GetValue(objModel, null);
139        }

140        dt.Rows.Add(row);
141
142        return dt;
143    }

144
145    /// <summary>
146    /// 对象转换为DataTable,并有多行DataRow
147    /// </summary>
148    /// <param name="objModel">有数据的对象</param>
149    /// <returns></returns>

150    public DataTable ObjectConvertDataTableWidthRows(cXTM_User[] objModel)
151    {
152        return ObjectConvertDataTableWidthRows(objModel, "");
153    }

154
155    /// <summary>
156    /// 对象转换为DataTable,并有多行DataRow
157    /// </summary>
158    /// <param name="objModel">有数据的对象</param>
159    /// <returns></returns>

160    public DataTable ObjectConvertDataTableWidthRows(cXTM_User[] objModel, string DataMapper)
161    {
162        Type entitytype = Type.GetType(objModel.GetType().AssemblyQualifiedName);
163        DataTable dt = new DataTable();
164        if (DataMapper != "")
165        {
166            dt = new DataTable(DataMapper);
167        }

168        if (objModel.Length == 0)
169        {
170            return dt;
171        }

172        dt.Columns.Clear();
173        for (int j = 0; j < objModel[0].Propertylist.Length; j++)
174        {
175            PropertyInfo propertyinfo = entitytype.GetProperty(objModel[0].Propertylist[j]);
176            dt.Columns.Add(new DataColumn(objModel[0].Propertylist[j], propertyinfo.GetType()));
177        }

178
179        for (int i = 0; i < objModel.Length; i++)
180        {
181            DataRow row = dt.NewRow();
182            for (int j = 0; j < objModel[i].Propertylist.Length; j++)
183            {
184                PropertyInfo propertyinfo = entitytype.GetProperty(objModel[i].Propertylist[j]);
185                row[objModel[i].Propertylist[j]] = propertyinfo.GetValue(objModel[i], null);
186            }

187            dt.Rows.Add(row);
188        }

189        return dt;
190    }

191    #endregion

192
193    #region 通用函数
194
195    /// <summary>
196    /// 转换为DataTable
197    /// </summary>
198    /// <param name="Source">数据源</param>
199    /// <param name="DataMember">数据表名称</param>

200    public static DataTable ConvertDataTable(object Source, string DataMember)
201    {
202        DataTable baseTable = new DataTable();
203        if (Source is DataTable)
204        {
205            baseTable = (DataTable)Source;
206            return baseTable;
207        }

208        if (Source is DataSet)
209        {
210
211            DataSet set1 = (DataSet)Source;
212            if ((set1.Tables.Count > 1&& ((DataMember == null|| (DataMember == "")))
213            {
214                throw new Exception("If there is more than one table in your dataset, you must define the DataMember property to specify which table to use.");
215            }

216            if (set1.Tables.Count < 1)
217            {
218                throw new Exception("There are no tables in the datasource.");
219            }

220            if ((DataMember != null&& (DataMember != ""))
221            {
222                baseTable = set1.Tables[DataMember];
223                return baseTable;
224            }

225            else
226            {
227                baseTable = set1.Tables[0];
228                return baseTable;
229            }

230
231        }

232        return baseTable;
233    }

234
235    /// <summary>
236    /// 返回DataTable为哈希表键值对
237    /// </summary>
238    /// <param name="SourceTable">数据行对象</param>
239    /// <returns>填充后哈希表</returns>

240    public static Hashtable DataRowConvertHashtable(DataRow SourceRow)
241    {
242        Hashtable hTable = new Hashtable();
243        IList list = SourceRow.ItemArray;
244        object[] tObj = new object[SourceRow.Table.Columns.Count];
245
246        for (int i = 0; i < SourceRow.Table.Columns.Count; i++)
247        {
248            tObj[SourceRow.Table.Columns.IndexOf(SourceRow.Table.Columns[i].ColumnName)] = SourceRow.Table.Columns[i].ColumnName;
249        }

250
251        for (int x = 0; x < list.Count; x++)
252        {
253            hTable.Add(tObj[x].ToString(), list[x]);
254        }

255        return hTable;
256    }

257
258    #endregion

259
260
261
262
263}

264
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏