首页 > 其他 > 详细

DataRow[]转DataTable

时间:2015-05-21 12:49:17      阅读:193      评论:0      收藏:0      [点我收藏+]

   DataRow[]转DataTable 经常忘记了会直接Add(DataRow),这样会报以下错误”此行已属于另一个表“,要用DataRow.ItemArray

  错误写法:

  

        public DataTable ToDataTable(DataRow[] rows)
        {
            if (rows == null || rows.Length == 0) return null;
            DataTable tmp = rows[0].Table.Clone();  // 复制DataRow的表结构
            foreach (DataRow row in rows)
                tmp.Rows.Add(row);  // 将DataRow添加到DataTable中
            return tmp;
        }

正确写法:


        public DataTable ToDataTable(DataRow[] rows)
        {
            if (rows == null || rows.Length == 0) return null;
            DataTable tmp = rows[0].Table.Clone();  // 复制DataRow的表结构
            foreach (DataRow row in rows)
                tmp.Rows.Add(row.ItemArray);  // 将DataRow添加到DataTable中
            return tmp;
        }


DataRow[]转DataTable

原文:http://blog.csdn.net/wrfccl/article/details/45888877

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!