类中的字段类型需要和数据库的字段类型匹配,大小写无关. 类中自定义的字段(数据库没有)将无法从datatable中取值
以下是源代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <SUMMARY>
/// 依据反射机制将DataTable中指定行数据赋给obj
/// </SUMMARY>
/// <PARAM name="obj" />要实例的对象
/// <PARAM name="dataTable" />DataTable
/// <PARAM name="row" />要读取DataTable行的序号
public static void InstanDataTableToBean(Object obj, DataTable dataTable, int row)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//指定行不存在
if (dataTable.Rows.Count < (row + 1))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception("指定行不存在!");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//DataTable列为空!
if (dataTable.Columns.Count < 1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception("DataTable列为空!");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Type type = obj.GetType(); //获取对象类型
PropertyInfo[] pInfos = type.GetProperties();
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for (int i = 0; i < dataTable.Columns.Count; i++) //循环所有列
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for (int j = 0; j < pInfos.Length; j++) //循环所有属性
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//比较的作用是防止数据库列名的大小写和属性的大小写不一致
if (dataTable.Columns[i].ColumnName.ToLower() == pInfos[j].Name.ToLower())
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
PropertyInfo pInfo = type.GetProperty(pInfos[j].Name); //反射机制,动态检索类属性
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
object colValue = dataTable.Rows[row][i];
//不同的基本数据类型,比如用Convert方法转换成对应类型的值
if (!Convert.IsDBNull(colValue) && colValue.ToString() != null) //如果数据集中此列有值
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (pInfos[j].PropertyType.FullName == "System.String")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pInfo.SetValue(obj, Convert.ToString(colValue), null);
}
else if (pInfos[j].PropertyType.FullName == "System.Int32")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pInfo.SetValue(obj, Convert.ToInt32(colValue), null);
}
else if (pInfos[j].PropertyType.FullName == "System.Int64")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pInfo.SetValue(obj, Convert.ToInt64(colValue), null);
}
else if (pInfos[j].PropertyType.FullName == "System.Single")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pInfo.SetValue(obj, Convert.ToSingle(colValue), null);
}
else if (pInfos[j].PropertyType.FullName =="System.Double")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pInfo.SetValue(obj, Convert.ToDouble(colValue), null);
}
else if (pInfos[j].PropertyType.FullName =="System.Decimal")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pInfo.SetValue(obj, Convert.ToDecimal(colValue), null);
}
else if (pInfos[j].PropertyType.FullName =="System.Char")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pInfo.SetValue(obj, Convert.ToChar(colValue), null);
}
else if (pInfos[j].PropertyType.FullName == "System.DateTime")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pInfo.SetValue(obj, Convert.ToDateTime(colValue), null);
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception("不支持的属性基本类型!");
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pInfo.SetValue(obj, null, null); //如果数据集中此列没有值
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
break;
}
}
}
}
catch (Exception ex)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{ //不捕获异常,异常抛出由开发人员处理
throw ex;
}
}