在c#中更改数据表对象的数据类型 [英] change datatype of a datatable object in c#

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

问题描述

如何在已经有数据的情况下更改c#中数据类型的数据类型。

我有一个int字段必须是typeof(字符串)

How to change the datatype of a datatbale in c# while already having data in.
I have an int field that has to be typeof(string)

推荐答案

亲爱的Digimanu,



数据填充后,您无法更改数据表的数据类型。



如需参考,请点击以下链接: -



http://stackoverflow.com/questions/2538477/changing-populated-datatable-column-data-types [ ^ ]



希望这会帮助你。



谢谢
Dear Digimanu,

You can't change the data type of a datatable once the data is populated into it.

For your reference follow the link:-

http://stackoverflow.com/questions/2538477/changing-populated-datatable-column-data-types[^]

Hope this will help you out.

Thanks


首先,我必须承认我不会理解为什么你不首先创建具有适当数据类型的列。这样可以省去很多麻烦。



但是,对于您的问题,您可以使用datatablereader将数据加载到新的数据表中。请考虑以下事项:

First, I must admit that I don't understand why you don't create the columns with proper data types in the first place. That would save a lot of trouble.

However, to your question, you can for example load the data into a new datatable using datatablereader. Consider the following:
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);


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

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