⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectmapper.cs

📁 HR系统,人事,能力素质,考勤,薪资等,AJAX,ASP.NET
💻 CS
字号:
using System;
using System.Data;
using System.Reflection;
using System.Collections;
using System.Configuration;

namespace iSun.SPL.ORM
{
	/// <summary>
	/// 公司:iSun软件工作室
	/// 通过查询数据表信息返回实体类对象
	/// 作者 :江怀玉
	/// Email:iSunStudio@Yahoo.com.cn
	/// 日期 :2006-05-10
	/// </summary>
	public class ObjectMapper
	{
		public ObjectMapper(object obj,string sqlcomm)
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
			this._obj = obj;
			this._sqlcomm = sqlcomm;
		}

		public ObjectMapper(object obj)
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
			this._obj = obj;
		}



		/// <summary>
		/// 通过查询语句,返回实体数组;
		/// </summary>
		/// <returns></returns>
		public object[] DatatableToObject()
		{
			return  DatatableToObject(this.GetDataBySqlComm());
		}
		/// <summary>
		/// 通过查询结果,返回实体数组;
		/// </summary>
		/// <param name="dt">查询结果</param>
		/// <returns>实体对象</returns>
		public object[] DatatableToObject(DataTable dt)
		{
			if (dt.Rows.Count==0)
			{
				return null;
			}

			Type t = this.Obj.GetType();
			PropertyInfo[] Mypropertyinfos =  t.GetProperties();
			ArrayList al = new ArrayList();
			for(int i = 0;i<dt.Rows.Count;i++)
			{
				object _obj = System.Reflection.Assembly.GetAssembly(t).CreateInstance(t.ToString());
				foreach(PropertyInfo pi in Mypropertyinfos)
				{
					try
					{
						iSun.Common.ReflectionUntil.SetPropertyValue(_obj,pi.Name,dt.Rows[i].ItemArray[dt.Columns[pi.Name].Ordinal].ToString());
					}
					catch(System.Exception e)
					{
						//log
                        throw e;
					}
				}
				al.Add(_obj);
			}
			return al.ToArray();
		}



		/// <summary>
		///  通过查询结果,返回单个实体对象;
		/// </summary>
		/// <returns></returns>
		public object DataTableToObject()
		{
			return  DataTableToObject(this.GetDataBySqlComm());
		}
		/// <summary>
		/// 通过查询结果,返回单个实体对象;
		/// </summary>
		/// <param name="dt">查询结果</param>
		/// <returns>实体对象</returns>
		public object DataTableToObject(DataTable dt)
		{
			if (dt.Rows.Count==0)
			{
				return null;
			}
			
			Type t = this.Obj.GetType();
			PropertyInfo[] Mypropertyinfos =  t.GetProperties();

			for(int i = 0;i<dt.Rows.Count;i++)
			{
				foreach(PropertyInfo pi in Mypropertyinfos)
				{
					try
					{
						iSun.Common.ReflectionUntil.SetPropertyValue(this.Obj,pi.Name,dt.Rows[i].ItemArray[dt.Columns[pi.Name].Ordinal].ToString());
					}
					catch (System.Exception e)
					{
						//throw e;
					}
				}
			}
			return this.Obj;
		}
		

		private object _obj;
		public object Obj
		{
			get {return _obj; }
			set {_obj = value;}
		}

		private string _sqlcomm;
		public string  SqlComm
		{
			get {return _sqlcomm; }
			set {_sqlcomm = value;}
		}

		/// <summary>
		/// 通过查询命令获取数据结果
		/// </summary>
		/// <returns>DataTable</returns>
		private DataTable GetDataBySqlComm()
		{
			return new iSun.DbDAL.DbSql().ExecuteQuery(this.SqlComm);
		}

		public string BuildXmlFile()
		{
			string xmlpath = System.Guid.NewGuid().ToString()+".Xml";
			xmlpath = ConfigurationSettings.AppSettings["webroot"] + "QueryCenter\\QueryXML\\"+xmlpath;
			new iSun.DbDAL.DbSql().WriteToXml(this.SqlComm,xmlpath);
			return xmlpath;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -