如何改变类型的数据表列 [英] How to change type datatable column

查看:126
本文介绍了如何改变类型的数据表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
如何转换数据表列类型?

我们怎样才能改变数据表中的列类型,如果表已经具有价值?

How can we change the datatable column type if table already have value?

推荐答案

数据表后更改数据类型得到填补是不可能的,但工作之一比比皆是,这是克隆的数据表和变型

Changing data type of datatable after get filled is not possible but one of the work abound for this is clone datatable and change type

第一种方式

//clone datatable     
DataTable dtCloned = dt.Clone();
//change data type of column
dtCloned.Columns[0].DataType = typeof(Int32);
//import row to cloned datatable
foreach (DataRow row in dt.Rows) 
{
    dtCloned.ImportRow(row);
}






第二个方法

和其他的方式来做到这一点是这样的,先填写架构 - 不是改变列数据类型,比填充确定时代

and other way to do it is like this , fill schema first - than change column datatype and than fill datable

adapter.FillSchema(table, SchemaType.Source);
table.Columns[0].DataType = typeof (Int32);
adapter.Fill(table);






第三条道路

做另一种方式是

System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("Col1", typeof(int));
dt.Rows.Add(1);

System.Data.DataTable dt2 = new System.Data.DataTable();
dt2.Columns.Add("Col1", typeof(string));
dt2.Load(dt.CreateDataReader(), System.Data.LoadOption.OverwriteChanges);

这篇关于如何改变类型的数据表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆