我可以使用 DataTables 将数据从一个数据库导入到另一个数据库吗?
我需要创建一个快速而肮脏的解决方案来将数据从数据库迁移到另一个数据库。这仅用于概念验证。从长远来看,我们将使用.NET 的同步框架。
数据库是相同的。该解决方案将用作 OCA(偶尔连接的应用程序)。
- 我读到他们想要从某些 XML 迁移到哪些表。
- 禁用每个表的目标上的所有约束。
- 对于他们想要从中迁移数据的每个表,我从源创建一个数据表。
- 创建一个指向目标的 DataTable。
- 将所有行从源导入到目标并插入它们
- 再次启用目标表上的所有约束。
我不确定以上是否可行。我已经完成了大部分工作,并且正在克隆源数据表。然后我遇到了克隆数据表没有指向任何地方的问题。
- 我可以将其指向目标然后插入吗?
- 有更好的方法吗?
- 另一种方法是创建 INSERT INTO 语句,使用元数据来标识标识列,而不将它们包含在列名称中。
I need to create a quick and dirty solution to migrate data from database into another. This is only being used a proof of concept. Long term we will use .NET's Sync Framework.
The databases are identical. The solution is going to be used as an OCA (occasionally connected application).
- I read in which tables they want to migrate from some XML.
- Disable all constraints on the target for each table.
- For each table they want to migrate data from I create a DataTable from the source.
- Create a DataTable pointing to the target.
- Import all the rows from the source into the target and insert them
- Enable all constraints on the target tables again.
I am not sure if the above is possible. I had most of it working and I was cloning the source DataTable. I then had the problem where the cloned DataTable wasn't pointing anywhere.
- Can I point it to the target and then insert?
- Is there a better way to do this?
- The alternative is to create INSERT INTO statements, using metadata to identify identity columns and not include them in the column names.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你提出的建议应该有效。但您可能会发现使用 SqlBulkCopy 类。
(这是一个以代码为中心的解决方案)
What you're proposing should work. But you might find it easier (and you'll definitely see better performance) with the SqlBulkCopy class.
(This is a code-focused solution)
简短回答:您可以加载
DataTable
并使用不同的DataAdapter
将其保存到另一个数据库中。但是,对于少代码的方法,您可以使用 SQL Server 数据库发布工具包 为 此处说明。
Short answer: You can load your
DataTable
and save it into another database by using a differentDataAdapter
.But, for a code less approach, you can to use SQL Server Database Publishing Toolkit as stated here.
您可以使用Sql Server 导入和导出向导 (dtswizard.exe )。
它创建一个 Integration Services 包,然后您可以随时保存并执行该包。
You can use the Sql Server Import and Export Wizard (dtswizard.exe).
It creates an Integration Services package that you can then save and execute whenever you want.