一个实例化的DATATABLE的类
来源:互联网 发布:网络的发展 编辑:程序博客网 时间:2024/04/28 09:36
silverlight调用webservice,关键是调用类的实体。而原先编程基于dataSet的模式。因此必要提出了dataset与实体之间的转化方法:
List<灌溉计划> jh = new List<灌溉计划>();
string sql = "select * from [灌溉计划]";
DataSet ds = dm.getsql(sql);
jh=USTC.Func.ConvertDataTableToEntityCollections<灌溉计划>(ds.Tables[0]);//调用下面方法即可
return jh.ToArray();
其中的灌溉计划是数据库表的实体类,可以通过直接建立linq的表实体,拖曳而成型
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
namespace USTC
{
/// <summary>
/// 数据服务基类对象
/// </summary>
public class DataServiceBase
{
DM dm = new DM();
#region Tabel 类型转换相关
/// <summary>
/// 转化Table成一个相对应的实体类集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="table"></param>
/// <returns></returns>
public List<T> ConvertDataTableToEntityCollections<T>(DataTable table)
where T : class, new()
{
if (table != null)
{
Type tc = typeof(T);
List<T> list = new List<T>(table.Rows.Count);
foreach (DataRow row in table.Rows)
{
T t = new T();
foreach (PropertyInfo pInfo in tc.GetProperties())
{
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;
}
/// <summary>
/// 得到指定SQL执行的结果并返回结果的实体类集合对象
/// 本方法综合了 ConvertDataTableToEntityCollections/ExecuteSQL 方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sql">待执行的SQL</param>
/// <returns></returns>
public List<T> GetEntityConllectionsFromSQL<T>(string sql)
where T : class, new()
{
if (string.IsNullOrEmpty(sql))
return null;
else
return ConvertDataTableToEntityCollections<T>(dm.getsql(sql).Tables[0]);
}
/// <summary>
/// 得到指定类型公开的属性
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private 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;
}
/// <summary>
/// 类型转换
/// FROM: http://www.cnblogs.com/cnee5/archive/2006/05/21/405403.html
/// </summary>
/// <param name="value"></param>
/// <param name="conversionType"></param>
/// <returns></returns>
protected 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();
/// <summary>
/// 实体类属性缓存
/// </summary>
private static Dictionary<Type, PropertyInfo[]> _PropertyInfoCache = new Dictionary<Type, PropertyInfo[]>();
#endregion
}
}
- 一个实例化的DATATABLE的类
- jsf中dataTable嵌套dataTable的实例
- 使用反射机制用一个类实例的值为DataTable添加一行
- 使用反射机制将一个类实例的值赋给DataTable某一行
- 转一个dataTable的使用
- 一个DataTable的数据如何导入另外一个DataTable中!!
- 筛选一个DataTable的数据,赋值给另外一个DataTable
- 筛选一个DataTable的数据,赋值给另外一个DataTable
- asp.net 构造datatable数据源的实例
- 关于DataTable中参照实例的注意事项
- 一个DataTable赋值给另一个DataTable的常用方法
- 创建一个DataTable以及遍历DataTable的值
- 一个DataTable的行列转换代码
- 一个DataTable的行列转换代码
- 一个dataTable的简单实现思路
- 一个完整的jQuery datatable示例
- C# 复制一个DataTable的数据行
- 实例化一个类的步骤
- [转]Android Power Management
- 认清3G的真正面目
- SQL查询每所学校语文成绩最高的学生信息
- A页面打开B页面,B页面提交数据并自动关闭
- 外贸企业自主建站要注意的问题
- 一个实例化的DATATABLE的类
- 查资料进来的
- 有关内存的思考题
- 宏定义好处
- 质量审计(Quality Audit)
- bash shell字符操作
- 朴素的解答-为什么进行知识管理?
- TD-SCDMA介绍
- Ubuntu8.1下安装JDK,配置 Java运行环境