不使用 .Copy 方法复制 DataTable
我有一个数据表,人们将其连接到不同的列更改事件等...
我需要偶尔清除该表并用来自不同数据表的新数据填充它。
我只是使用 targetTable = sourceTable.Copy(),但后来我上面的人丢失了他们连接到的事件。
我想我可以为 DataTables 编写一个扩展方法来执行此操作,这样如果情况再次出现,我可以重用它。
我遇到的问题是,如果我以通用方式执行此操作,那么我想将架构从源表完全传输到目标表。
有什么简单的方法可以做到这一点吗? 我只是想比较列以确保名称/类型匹配,但后来我想如果我也可以传输架构,我可以使其更加健壮。
看起来有一种方法可以通过使用 ReadXMLSchema 和 WriteXMLSchema 来传输架构,但我想知道它们是否不是一种更简单的方法。
I have a DataTable that's had people wire into it's different column changed events, ect...
I need to clear the table out occasionally and fill it with new data, from a different datatable.
I was just using targetTable = sourceTable.Copy(), but then the people above me loose the events they've wired into.
I was thinking that I could write an extension method for DataTables to do this, so I could reuse it if the situation ever arose again.
The part where I get hung up is that if I'm doing it in a generic way, then I want to completely transfer the schema from the source table to the target table.
Is there any easy way of doing this?
I was looking at just comparing the columns to make sure the names/types matched, but then I thought I could make it more robust if I could also transfer the schema.
It looks like there is a way to transfer the schema by using the ReadXMLSchema and WriteXMLSchema, but I'm wondering if their isn't an eaiser way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://msdn.microsoft.com/en-我们/library/system.data.datatable.merge.aspx
传输架构和所有数据,并根据需要保留更改或行状态。
http://msdn.microsoft.com/en-us/library/system.data.datatable.merge.aspx
transfers the schema and all data, preserving changes or row states if you need.
您是否尝试过
YourTable.Clear() ——只是删除记录,但没有任何其他定义
YourTable.Merge(TheOtherTable)——将记录从一个表合并到另一个表中。
Have you tried
YourTable.Clear() -- just to remove records, but no any other definitions
YourTable.Merge( TheOtherTable) -- to merge records from one into the other.