使用数据集将数据从 1Db 复制到另一个
抱歉,如果我的英语不太好,我会尽力解释这个问题。
我需要将所有数据从 Acces mdb 表(与 OleDB 连接)复制到 MySql DB 表(与 ODBC 连接)
我做了一个工作解决方案,但它非常慢,所以我想尝试其他解决方案检查他们是否可以给我更多的表现。
连接到mdb的解决方案,使用DataReader,然后对于Datareader中的每一行,我将INSERT插入到Mysql表中(在复制之前,我截断表以将其清空)
记录超过10K,并且此操作非常慢,我确实需要在其他两张桌子上做同样的事情,这两张桌子也和这张桌子一样大。
我无法进行直接 sql 插入(如 INSERT INTO A in ..... SELECT * FROM B),因为 1 个 DB 有一个 OleDB 连接,另一个有一个 ODBC 连接。
所以我想尝试使用 TableAdapters 和 DataSet 来进行此操作,但我无法使其工作。
问题是数据集的 HasChanges
是 false
如果您需要一些代码,我可以发布,但我所做的如下:
- 连接到 MDb
- 创建 OleDbTableAdapter
- 创建数据集
- 用 TableAdapter 填充数据集
- 连接到 MySqlDB
- 创建 ODBCTableAdapter
- 使用第一个数据集的 ODBCTableAdapter 的更新命令
。但是 DS 没有提交任何更改,所以他没有向数据库写入任何内容,所以我想使用另一个数据集并将数据从 DS1 复制到 DS2 以添加行,看看 has.changes 是否为 true 以及使用 DS2 的 ODBCTableadapter 的更新命令。
我尝试在数据集之间复制数据:
ds2 = ds1.copy
我还尝试使用数据集导入功能,循环 DS1
数据行并将所有行从 DS1
导入到 DS2
。 在这两种情况下,行都会添加到 DS2,但 HasChanges
仍然为 false,我该怎么办? 只是为了澄清可能的问题,我没有使用 DS.Acceptchanges
,定义了 PrimaryKey,定义了 UpdateCommand
,DS 有数据(我填充 2 个 DataGrid 来检查它)。 没有给出错误,只是没有数据写入数据库。
有什么建议吗?谢谢指教。
Sorry if my english is not so good, I will try to explain the problem.
I need to copy all data from a Table of a Acces mdb (connected with OleDB) to a Table of a MySql DB (connected with ODBC)
I made a working solution, but it's very slow, and so I want to try other solutions to check if they can give me more performance.
The solution connecting to the mdb, using a DataReader, then for each row in Datareader I make an INSERT into the Mysql Table ( before copy I truncate the table to get it empty)
The records are more than 10K and this operation is very slow, and do be onest I need to do the same thing on other 2 tables also very big as this one.
I cannot make a direct sql insert ( as INSERT INTO A in ..... SELECT * FROM B
) because the 1 DB has a OleDB conn and the other has a ODBC conn.
So I thought to try to make this operation using TableAdapters
and DataSet
, but I'm not able to make it working.
The problem is that the Dataset's HasChanges
is false
If you need some code I can post, but what I do is following:
- Connection to MDb
- Create OleDbTableAdapter
- Create DataSet
- Fill DataSet with TableAdapter
- Connection to MySqlDB
- Create ODBCTableAdapter
- Using Update command of ODBCTableAdapter with the first Dataset.
But DS has no changes commited so he don't write anything to DB, so I thought to use another Dataset and copy data from DS1 to DS2 to add rows, see if has.changes was true and the making Update command of ODBCTableadapter using DS2.
I tryed to copy data between datasets:
ds2 = ds1.copy
I tried also to use dataset import function, looping DS1
datarows and Importing all rows from DS1
to DS2
.
In both cases the rows are added to DS2, but still HasChanges
is false, what can I do?
Just to clarify possible questions I didn't use DS.Acceptchanges
, PrimaryKey is defined, The UpdateCommand
is defined, DS has data (I populate 2 DataGrids to check it).
No errors given, just no data written on DB.
Any suggestion? Thanks in advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以大大加快该过程的唯一方法是批处理 SQL 更新命令。否则,每个更新请求将一次执行一个。
您可能需要考虑 MySQL LOAD DATA INFILE 命令方便大量数据的快速导入。
我无法找到与 MS SQL Server 的 SqlBulkCopy 类,但如果您的 MySQL 库支持类似的函数,您可能也会感兴趣。
一般来说,生成的网络流量越少,执行大型数据库插入的速度就越快(尽管在某一点之后可能存在其他限制因素)。
The only way you can speed up the process in a great deal would be to batch your SQL update commands. Otherwise each update request will be executed one at a time.
You might want to consider the MySQL LOAD DATA INFILE command to facilitate the rapid import of large amounts of data.
I wasn't able to locate a function comparable to MS SQL Server's SqlBulkCopy class, but if your MySQL library supports a similar function that may be of interest as well.
In general, the less network traffic you generate the faster you'll be able to perform large database inserts (although there can be other limiting factors after a point).