标签:
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; }
标签:
原文地址:http://blog.csdn.net/wrfccl/article/details/45888877