The SqlBulkCopy class can be used to write data only to SQL Server tables. The code samples shown in this topic use the SQL Server sample database, AdventureWorks. To avoid altering the existing tables code samples write data to tables that you must create first.
The BulkCopyDemoMatchingColumns and BulkCopyDemoDifferentColumns tables are both based on the AdventureWorksProduction.Products table. In code samples that use these tables, data is added from the Production.Products table to one of these sample tables. The BulkCopyDemoDifferentColumns table is used when the sample illustrates how to map columns from the source data to the destination table; BulkCopyDemoMatchingColumns is used for most other samples.
A few of the code samples demonstrate how to use one SqlBulkCopy class to write to multiple tables. For these samples, the BulkCopyDemoOrderHeader and BulkCopyDemoOrderDetail tables are used as the destination tables. These tables are based on the Sales.SalesOrderHeader and Sales.SalesOrderDetail tables in AdventureWorks.
Note
The SqlBulkCopy code samples are provided to demonstrate the syntax for using SqlBulkCopy only. If the source and destination tables are located in the same SQL Server instance, it is easier and faster to use a Transact-SQL INSERT … SELECT statement to copy the data.
Table Setup
To create the tables necessary for the code samples to run correctly, you must run the following Transact-SQL statements in a SQL Server database.
SQL
USE AdventureWorks
IFEXISTS (SELECT * FROM dbo.sysobjects
WHEREid = object_id(N'[dbo].[BulkCopyDemoMatchingColumns]')
AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
DROPTABLE [dbo].[BulkCopyDemoMatchingColumns]
CREATETABLE [dbo].[BulkCopyDemoMatchingColumns]([ProductID] [int] IDENTITY(1,1) NOTNULL,
[Name] [nvarchar](50) NOTNULL,
[ProductNumber] [nvarchar](25) NOTNULL,
CONSTRAINT [PK_ProductID] PRIMARY KEY CLUSTERED
(
[ProductID] ASC
) ON [PRIMARY]) ON [PRIMARY]
IFEXISTS (SELECT * FROM dbo.sysobjects
WHEREid = object_id(N'[dbo].[BulkCopyDemoDifferentColumns]')
AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
DROPTABLE [dbo].[BulkCopyDemoDifferentColumns]
CREATETABLE [dbo].[BulkCopyDemoDifferentColumns]([ProdID] [int] IDENTITY(1,1) NOTNULL,
[ProdNum] [nvarchar](25) NOTNULL,
[ProdName] [nvarchar](50) NOTNULL,
CONSTRAINT [PK_ProdID] PRIMARY KEY CLUSTERED
(
[ProdID] ASC
) ON [PRIMARY]) ON [PRIMARY]
IFEXISTS (SELECT * FROM dbo.sysobjects
WHEREid = object_id(N'[dbo].[BulkCopyDemoOrderHeader]')
AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
DROPTABLE [dbo].[BulkCopyDemoOrderHeader]
CREATETABLE [dbo].[BulkCopyDemoOrderHeader]([SalesOrderID] [int] IDENTITY(1,1) NOTNULL,
[OrderDate] [datetime] NOTNULL,
[AccountNumber] [nvarchar](15) NULL,
CONSTRAINT [PK_SalesOrderID] PRIMARY KEY CLUSTERED
(
[SalesOrderID] ASC
) ON [PRIMARY]) ON [PRIMARY]
IFEXISTS (SELECT * FROM dbo.sysobjects
WHEREid = object_id(N'[dbo].[BulkCopyDemoOrderDetail]')
AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
DROPTABLE [dbo].[BulkCopyDemoOrderDetail]
CREATETABLE [dbo].[BulkCopyDemoOrderDetail]([SalesOrderID] [int] NOTNULL,
[SalesOrderDetailID] [int] NOTNULL,
[OrderQty] [smallint] NOTNULL,
[ProductID] [int] NOTNULL,
[UnitPrice] [money] NOTNULL,
CONSTRAINT [PK_LineNumber] PRIMARY KEY CLUSTERED
(
[SalesOrderID] ASC,
[SalesOrderDetailID] ASC
) ON [PRIMARY]) ON [PRIMARY]
Do you want to know how to create new tables in Business Central? If so, this module is for you. This module focuses on the different table types in Business Central and show you how to create new tables. Additionally, you learn how to use Visual Studio Code snippets to create a table, fields, and keys.