private DataTable CovertDataType(DataTable orgDataTable, string colNames, string convertCol, YstPack.Common.ColumnType convertDataType)
{
DataTable dt = new DataTable();
//克隆表结构
dt = orgDataTable.Clone();
foreach (DataColumn column in dt.Columns)
{
if (column.ColumnName == convertCol)
{
//修改列类型
column.DataType = typeof(Guid);
}
}
string[] colArry = colNames.Split(',');
foreach (DataRow dr in orgDataTable.Rows)
{
DataRow newdr = dt.NewRow();
foreach (string str in colArry)
{
if (str == convertCol)//转换列数据类型
{
switch (convertDataType)
{
case YstPack.Common.ColumnType.唯一标识:
newdr[str] = new Guid(dr[str].ToString());
break;
default:
break;
}
}
else
{
newdr[str] = dr[str];//不需要转化,直接赋值,但是这里就抛出异常!
}
}
dt.Rows.Add(newdr);
}
return dt;
}
运行 foreach (string str in colArry)循环第一次能成功,但是第二次就报列“X”不属于表异常
把异常放出来啊