using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
namespace Syit.AHSZY.Mobile.DataServices
{
///
/// 数据服务基类对象
///
public class DataServiceBase
{
#region DB 处理
static USTC.OracleDbc db;
///
/// DB
///
private static USTC.OracleDbc Db
{
get
{
if (db == null)
{
DM dm = new DM();
db = dm.getDataBase();
}
return db;
}
}
///
/// 执行SQL字符串返回一个DataTable
///
///
///
protected static DataTable ExecuteSQL(string sql)
{
Db.open();
DataSet ds = (DataSet)Db.getData(sql, false);
Db.close();
if (ds != null && ds.Tables.Count != 0)
return ds.Tables[0];
else
return null;
}
#endregion
#region Tabel 类型转换相关
///
/// 转化Table成一个相对应的实体类集合
///
///
///
///
protected static List ConvertDataTableToEntityCollections (DataTable table)
where T : class, new()
{
if (table != null)
{
PropertyInfo[] pInfos = GetTypePropertyInfo(typeof(T));
List list = new List (table.Rows.Count);
foreach (DataRow row in table.Rows)
{
T t = new T();
foreach (PropertyInfo pInfo in pInfos)
{
if (table.Columns.Contains(pInfo.Name))
{
//HACK: 对象转换成指定的类型使用 Convert.ChangeType
// Convert.ChangeType(row[pInfo.Name], pInfo.PropertyType);
try
{
pInfo.SetValue(t, ChangeType(row[pInfo.Name], pInfo.PropertyType), null);
}
catch (Exception ex)
{
throw ex;
}
}
}
list.Add(t);
}
return list;
}
return null;
}
///
/// 得到指定SQL执行的结果并返回结果的实体类集合对象
/// 本方法综合了 ConvertDataTableToEntityCollections/ExecuteSQL 方法
///
///
/// 待执行的SQL
///
protected static List GetEntityConllectionsFromSQL (string sql)
where T : class, new()
{
if (string.IsNullOrEmpty(sql))
return null;
else
return ConvertDataTableToEntityCollections (ExecuteSQL(sql));
}
///
/// 得到指定类型公开的属性
///
///
///
private static PropertyInfo[] GetTypePropertyInfo(Type type)
{
PropertyInfo[] infos = null;
//锁定防止 多次 ADD 进缓存
lock (_PROPERTYINFO_READ_LOCK)
{
_PropertyInfoCache.TryGetValue(type, out infos);
if (infos == null)
{
infos = type.GetProperties();
_PropertyInfoCache.Add(type, infos);
}
}
return infos;
}
///
/// 类型转换
/// FROM: http://www.cnblogs.com/cnee5/archive/2006/05/21/405403.html
///
///
///
///
public static object ChangeType(object value, Type conversionType)
{
//INFO 对DBNull类型特殊处理
if (Convert.IsDBNull(value))
{
//非可空类型的值类型处理
if (!(conversionType.IsGenericType &&
conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))))
{
if (!conversionType.IsValueType)
return null;
else
return Activator.CreateInstance(conversionType);
}
else
return null;
}
//可空类型类型处理
if (conversionType.IsGenericType &&
conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
if (value == null)
return null;
System.ComponentModel.NullableConverter nullableConverter
= new System.ComponentModel.NullableConverter(conversionType);
conversionType = nullableConverter.UnderlyingType;
}
return Convert.ChangeType(value, conversionType);
}
private static object _PROPERTYINFO_READ_LOCK = new object();
///
/// 实体类属性缓存
///
private static Dictionary _PropertyInfoCache = new Dictionary ();
#endregion
}
}
===============================================================以上代码,工作的目的是代替
dataSetXML.DataSet1TableAdapters.Mytool dt = new dataSetXML.DataSet1TableAdapters.Mytool();
DataSet1._MsgDataTable dt1 = dt.GetData();
this.dataGridView1.DataSource = dt1;