如何创建数据表副本 [英] How can create a Copy of Datatable
本文介绍了如何创建数据表副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Web应用程序中,将DB值分配给一个数据表,然后将其分配给另一个数据表对象.但是,当我们编辑时,复制的数据表对象中也会发生同样的变化.
In my web application assign DB values to a Datatable and it assigned to another datatable object. But when we edit this same change occur in copied datatable object.
Datatable dt=new DaTatable()
Datatable dtcopy=new DaTatable()
dt=obj.GetCountry();
dtcopy=dt;
dt.columns["NameEng"].ColumnName = "Name";
现在,在datatble dt的"NameEng"列中将其更改为"Name".
但是在dtcopy中也会发生相同的变化.原因是什么,以及如何避免它.
Now in datatble dt''s column ''NameEng'' change to ''Name .
But same change occur in dtcopy. What is the reason and how can avoid it.
推荐答案
这是因为dtcopy和dt都引用相同的对象.
使用 DataTable.Copy方法 [
This is because both dtcopy and dt are referencing the same object.
Use DataTable.Copy Method[^] to copy from one datatable to another.
Instead of
dtcopy=dt;
使用
Use
dtcopy = dt.Copy();
复制数据表.
如果只需要数据表的结构而不是数据,则可以使用 DataTable.Clone方法 [^ ]
to copy the data table.
If you want only the structure of datatable and not the data you can use DataTable.Clone Method[^]
这篇关于如何创建数据表副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文