加载表结构并保持成XML

string cmdText = @"select   * from kb_lable_temp where 1=2";
using (SqlConnection conn = new SqlConnection(DBCtx.ConnStr))
{
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(cmdText,conn);
conn.Open();
using (var dr = cmd.ExecuteReader(CommandBehavior.SchemaOnly))
{
dt.Load(dr);
dt.WriteXmlSchema("C:\\xxx.xml");
} }
DataTable dt3 = new DataTable();
dt3.ReadXmlSchema("C:\\xxx.xml");

List<T>到DataTable

using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System;
using System.Collections;
namespace F.Studio.Util
{
public static class DataTableExtensions
{
/// <summary>
/// 转化一个DataTable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static DataTable ToDataTable<T>(this IEnumerable<T> list,params string[] tableName)
{
//创建属性的集合
List<PropertyInfo> pList = new List<PropertyInfo>();
//获得反射的入口
Type type = typeof(T);
string tname = "Table1";
if (tableName.Length >= )
{
tname = tableName[];
}
DataTable dt = new DataTable(tname);
//把所有的public属性加入到集合 并添加DataTable的列
Array.ForEach<PropertyInfo>(type.GetProperties(), p =>
{
pList.Add(p);
var theType=p.PropertyType;
//处理可空类型
if (theType.IsGenericType && theType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
dt.Columns.Add(p.Name,Nullable.GetUnderlyingType(theType));
}
else
{
dt.Columns.Add(p.Name, theType);
}
});
foreach (var item in list)
{
//创建一个DataRow实例
DataRow row = dt.NewRow();
//给row 赋值
pList.ForEach(p =>
{
var v=p.GetValue(item, null);
row[p.Name] = v==null ? DBNull.Value : v; });
//加入到DataTable
dt.Rows.Add(row);
}
return dt;
}
}
}

将List<T>转化成 DataTable--调整可空类型的转化错误的更多相关文章

  1. 将DataSet转化成XML格式的String类型,再转化回来。

    /// <summary> /// 获取DataSet的Xml格式 /// </summary> public static string GetDataSetXml(this ...

  2. DataTable调整顺序

    DataTable中手动调整列的顺序 DataTable中手动调整列的顺序(列序,reorder,Rearrange)DataTable dt = new DataTable(); dt.Column ...

  3. Excel转化成DataTable实现:NPOI和OLEDb

    使用两种方式实现的excel数据转化成DataSet,再结合前一篇的DataTable转化为实体,就可以解决excel到实体之间的转化. 代码如下: 首先定义一个接口: public interfac ...

  4. DataRow 数组转化成DataTable

    #region 封装DataTable DataTable dt = null; if (newRows.Length > 0) { dt = newRows[0].Table.Clone(); ...

  5. 过滤DataTable中的空数据

    DataTable dt = new DataTable(tableName); for (int i = 0; i < columnsNames.Length; i++) { dt.Colum ...

  6. 判断DataTable是否为空

    DataTable: ) { //为空的操作 } DataRow: if(!DataRow.IsNull("列名")) { //不为空的操作 }

  7. 解决DataTable中的DataColumn类型默认为int类型时, 导致不能修改其列值为其他类型的解决办法

    问题起因: 扔给数据库一条select * from [表名] , 得到一个DataTable, 发现有一列status状态的DataColumn的类型是int,然后我想换成字典表里的文字描述,然后就 ...

  8. DataTable转list时 可空类型的转换问题

    public class UtilHelper { public static IList<T> ConvertTo<T>(DataTable table) { if (tab ...

  9. datatable to entiy list 不支持可空类型和枚举类型

    还没有找到解决方法,暂存,希望有知道能告诉我.谢谢.

随机推荐

  1. JAVA中的策略模式

    现在我们有一个虚基类-鸭子(abstract Duck). 有真鸭子,野鸭子,橡皮鸭子继承了该类.虚基类有swing方法,毕竟游泳是所有的鸭子都应有的功能.还有一个虚方法display,这个方法在子类 ...

  2. ubuntu 14.10 安装 zabbix

    在ubuntu 14.10 上部署 zabbix 2.x 基本软件包安装 既然是ubuntu系统,当然要用好apt-get神器. 参考教程 URL:http://blog.csdn.net/cloud ...

  3. poj 1860 Currency Exchange :bellman-ford

    点击打开链接 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16635   Accept ...

  4. python正则表达式介绍

    点击打开链接 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的 ...

  5. python连接hiveserver2

    sudo pip install pyhs2 网上找的例子: #!/usr/bin/env python # -*- coding: utf-8 -*- # hive util with hive s ...

  6. 在开发板Linux上挂载"驱动"挂载不成功,出现提示server 172.27.52.100 not responding, still trying

    1.在开发板具体操作步骤如下:   1.1 :设置IP ifconfig eth0 172.27.52.200   1.2 :ping通 虚拟机Linux 主机Linux ping XXX.XXX.X ...

  7. linux spi驱动开发学习-----spidev.c和spi test app

    一.spidev.c文件 看一个设备驱动的方法: module_init标识的入口初始化函数spidev_init,(module_exit标识的出口函数) 设备与设备驱动匹配时候调用的probe方法 ...

  8. WPS去掉键入时自动进行句首字母大写更正

    1.单击左上角的菜单选项 2.选择上图中的“选项”按钮

  9. NoSuchMethodError: resolveTypeArguments

    NoSuchMethodError: resolveTypeArguments——因为spring版本冲突导致,观察解压war包后lib中有几个spring.在pom中通过exclusion解决 Ht ...

  10. Metrics.NET report to Zabbix

    废话不多说,先上git地址 https://github.com/binking338/Metrics.Reporters.ZabbixReporter 实现了Metrics.NET到Zabbix的报 ...