复制 SQL 数据的最快方法

发布于 2024-09-08 12:06:05 字数 206 浏览 0 评论 0原文

我可以使用一些建议/想法。 我编写了一个控制台应用程序,用于将 MS Access(我知道,但我继承了它)中的表中的所有数据查询到 SQL 表。它每天早上作为计划任务运行。两个表之间的字段不相同。目前,我从 MS Access 表中选择所有数据,循环访问数据集并将每一行插入 SQL 表中。我还在这个过程中写了一个快速日志文件。它有效,但速度不快。如果您有任何改进流程的想法,我将不胜感激。 谢谢!

I could use some suggestions / ideas.
I wrote a console application that queries all data from a table in MS Access (I know, but I inherited it) to a SQL table. It runs every morning as a scheduled task. The fields between the two tables are not identical. Currently I select all data from the MS Access table, loop through the dataset and insert each row into the SQL table. I also write a quick log file in the process. It works, but it's not fast. I would appreciate any ideas that you may have to improve the process.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆流 2024-09-15 12:06:05

SqlBulkCopy 类

这是方式< /strong> 比单个 insert 语句更快。

您必须为主键增加您自己的身份字段值。为此,首先获取上次停止的身份字段值:

select top 1 id_customer
来自客户
order by id_customer desc

然后在循环访问 DataSet 时递增 int 变量。

或者您可以使用 主键列的 GUID< /a> 相反。

示例代码:

public static void BulkLoadDataTable(DataTable table, string destinationTable)
{
  using (SqlBulkCopy bulkCopy = new SqlBulkCopy(_connectionString))
  {
    bulkCopy.BulkCopyTimeout = 1200;
    bulkCopy.DestinationTableName = destinationTable;
    bulkCopy.WriteToServer(table);
  }
}

使用 SqlBulkCopy:强类型数据表的

using System;
using System.Data;

public class CustomersDataTable : DataTable
{
  public CustomersDataTable() : base()
  {
    base.TableName = "Customers";
    Columns.Add(new DataColumn("id_customer", typeof(int)));
    Columns.Add(new DataColumn("first_name", typeof(string)));
    Columns.Add(new DataColumn("last_name", typeof(string)));
    Columns.Add(new DataColumn("active", typeof(bool)));
  }
}

SqlBulkCopy Class

It's way faster than individual insert statements.

You have to increment your own identity field value for the primary key. To do that, first grab the last identity field value where you left off:

select top 1 id_customer
from customers
order by id_customer desc

Then increment an int variable as you loop through your DataSet.

Or you can use GUID for primary key column instead.

Example code for using SqlBulkCopy:

public static void BulkLoadDataTable(DataTable table, string destinationTable)
{
  using (SqlBulkCopy bulkCopy = new SqlBulkCopy(_connectionString))
  {
    bulkCopy.BulkCopyTimeout = 1200;
    bulkCopy.DestinationTableName = destinationTable;
    bulkCopy.WriteToServer(table);
  }
}

Strongly Typed DataTable:

using System;
using System.Data;

public class CustomersDataTable : DataTable
{
  public CustomersDataTable() : base()
  {
    base.TableName = "Customers";
    Columns.Add(new DataColumn("id_customer", typeof(int)));
    Columns.Add(new DataColumn("first_name", typeof(string)));
    Columns.Add(new DataColumn("last_name", typeof(string)));
    Columns.Add(new DataColumn("active", typeof(bool)));
  }
}
淡莣 2024-09-15 12:06:05

你看过SSIS包吗?我会先调查一下。如果您无权访问它,请查看此处提到的 SqlBulkCopy 类。

您还可以考虑使用 带有 INSERT 的 SELECT 语句

Have you looked at SSIS packages? I would look into that first. If you don't have access to that, then look into the SqlBulkCopy class mentioned here already.

You could also look into using SELECT statement with an INSERT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文