SqlBulkCopy
Copies all rows in the supplied DataTable to a destination table
specified by the DestinationTableName property of the SqlBulkCopy object.
Remarks:
While
the bulk copy operation is in progress, the associated destination SqlConnection is busy serving it, and no other
operations can be performed on the connection.
Example:
The following
Console application demonstrates how to bulk load data from a DataTable. The destination table is a table in
the AdventureWorksdatabase.
This sample will not run unless you have
created the work tables as described in Bulk Copy Example Setup. This code is
provided to demonstrate the syntax for using SqlBulkCopy only.
If the source and destination tables are in the same SQL Server instance, it is
easier and faster to use a Transact-SQL INSERT … SELECT statement to copy the
data.
Imports System.Data.SqlClient
Module Module1
Sub Main()
Dim connectionString As String = GetConnectionString()
' Open a connection to the AdventureWorks database.
Using connection As SqlConnection = _
New
SqlConnection(connectionString)
connection.Open()
' Perform an initial count on the destination table.
Dim commandRowCount As New SqlCommand( _
"SELECT COUNT(*) FROM
dbo.BulkCopyDemoMatchingColumns;",
_
connection)
Dim countStart As Long = _
System.Convert.ToInt32(commandRowCount.ExecuteScalar())
Console.WriteLine("Starting row count = {0}", countStart)
' Create a table with some rows.
Dim newProducts As DataTable = MakeTable()
' Note that the column positions in the source DataTable
' match the column positions in the destination table,
' so there is no need to map columns.
Using bulkCopy As SqlBulkCopy = _
New SqlBulkCopy(connection)
bulkCopy.DestinationTableName = "dbo.BulkCopyDemoMatchingColumns"
Try
' Write from the source to the destination.
bulkCopy.WriteToServer(newProducts)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Using
' Perform a final count on the destination table
' to see how many rows were added.
Dim countEnd As Long = _
System.Convert.ToInt32(commandRowCount.ExecuteScalar())
Console.WriteLine("Ending row count
= {0}", countEnd)
Console.WriteLine("{0} rows were
added.", countEnd - countStart)
Console.WriteLine("Press Enter to
finish.")
Console.ReadLine()
End Using
End Sub
Private Function MakeTable() As DataTable
' Create a new DataTable named NewProducts.
Dim newProducts As DataTable = _
New DataTable("NewProducts")
' Add three column objects to the table.
Dim productID As DataColumn = New DataColumn()
productID.DataType =
System.Type.GetType("System.Int32")
productID.ColumnName
= "ProductID"
productID.AutoIncrement = True
newProducts.Columns.Add(productID)
Dim productName As DataColumn = New DataColumn()
productName.DataType
= System.Type.GetType("System.String")
productName.ColumnName = "Name"
newProducts.Columns.Add(productName)
Dim productNumber As DataColumn = New DataColumn()
productNumber.DataType
= System.Type.GetType("System.String")
productNumber.ColumnName = "ProductNumber"
newProducts.Columns.Add(productNumber)
' Create an array for DataColumn objects.
Dim keys(0) As DataColumn
keys(0) = productID
newProducts.PrimaryKey = keys
' Add some new rows to the collection.
Dim row As DataRow
row =
newProducts.NewRow()
row("Name") = "CC-101-WH"
row("ProductNumber")
= "Cyclocomputer - White"
newProducts.Rows.Add(row)
row =
newProducts.NewRow()
row("Name") = "CC-101-BK"
row("ProductNumber")
= "Cyclocomputer - Black"
newProducts.Rows.Add(row)
row =
newProducts.NewRow()
row("Name") = "CC-101-ST"
row("ProductNumber")
= "Cyclocomputer - Stainless"
newProducts.Rows.Add(row)
newProducts.AcceptChanges()
' Return the new DataTable.
Return newProducts
End Function
Private Function GetConnectionString() As String
' To avoid storing the connection string in your code,
' you can retrieve it from a configuration file.
Return "Data Source=(local);" & _
"Integrated Security=true;" & _
"Initial Catalog=AdventureWorks;"
End Function
End Module
Name
|
Description
|
|
|
Initializes a new instance of the SqlBulkCopy class using the specified open
instance of SqlConnection.
|
|
|
Initializes a new instance of the SqlBulkCopy class using the supplied existing
open instance ofSqlConnection. The SqlBulkCopy instance behaves according to
options supplied in the copyOptionsparameter.
If a non-null SqlTransaction is supplied, the copy operations
will be performed within that transaction.
|
|
|
Initializes and opens a new instance of SqlConnection based on the supplied connectionString. The
constructor uses the SqlConnection to initialize a new instance of the SqlBulkCopy class.
|
|
|
Initializes and opens a new instance of SqlConnection based on the supplied connectionString. The
constructor uses that SqlConnection to initialize a new instance of the SqlBulkCopy class. TheSqlConnection instance behaves according to
options supplied in the copyOptions parameter.
|
Properties
Name
|
Description
|
|
|
Number of rows in each batch. At the end of each batch,
the rows in the batch are sent to the server.
|
|
|
Number of seconds for the operation to complete before
it times out.
|
|
|
Returns a collection of SqlBulkCopyColumnMapping items. Column mappings define the
relationships between columns in the data source and columns in the
destination.
|
|
|
Name of the destination table on the server.
|
|
|
Enables or disables a SqlBulkCopy object
to stream data from an IDataReader object
|
|
|
Defines the number of rows to be processed before
generating a notification event.
|
Methods
Name
|
Description
|
|
|
Closes the SqlBulkCopy instance.
|
|
|
Determines whether the specified object is equal to the
current object.(Inherited from Object.)
|
|
|
Serves as the default hash function. (Inherited from Object.)
|
|
|
||
|
Returns a string that represents the current
object.(Inherited from Object.)
|
|
|
Copies all rows from the supplied DataRow array
to a destination table specified by theDestinationTableName property of the SqlBulkCopy object.
|
|
|
Copies all rows in the supplied DataTable to
a destination table specified by theDestinationTableName property of the SqlBulkCopy object.
|
|
|
Copies only rows that match the supplied row state in
the supplied DataTable to
a destination table specified by the DestinationTableName property of the SqlBulkCopy object.
|
|
|
Copies all rows from the supplied DbDataReader array to a destination table
specified by theDestinationTableName property of the SqlBulkCopy object.
|
|
|
Copies all rows in the supplied IDataReader to a destination table specified by
theDestinationTableName property of the SqlBulkCopy object.
|
|
|
The asynchronous version of WriteToServer, which copies all rows from
the supplied DataRow array
to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.
|
|
|
The asynchronous version of WriteToServer, which copies all rows from
the supplied DataRow array
to a destination table specified by the DestinationTableName property of the SqlBulkCopy object.
The cancellation token can be used to request that the
operation be abandoned before the command timeout elapses. Exceptions will be
reported via the returned Task object.
|
|
|
The asynchronous version of WriteToServer, which copies all rows in the
supplied DataTable to
a destination table specified by the DestinationTableName property of the SqlBulkCopy object.
|
|
|
The asynchronous version of WriteToServer, which copies all rows in the
supplied DataTable to
a destination table specified by the DestinationTableName property of the SqlBulkCopy object.
The cancellation token can be used to request that the
operation be abandoned before the command timeout elapses. Exceptions will be
reported via the returned Task object.
|
|
|
The asynchronous version of WriteToServer, which copies only rows that
match the supplied row state in the supplied DataTable to
a destination table specified by the DestinationTableNameproperty
of the SqlBulkCopy object.
|
|
|
The asynchronous version of WriteToServer, which copies only rows that
match the supplied row state in the supplied DataTable to
a destination table specified by the DestinationTableNameproperty
of the SqlBulkCopy object.
The cancellation token can be used to request that the
operation be abandoned before the command timeout elapses. Exceptions will be
reported via the returned Task object.
|
|
|
The asynchronous version of WriteToServer, which copies all rows from
the supplied DbDataReaderarray to a destination table
specified by the DestinationTableName property of the SqlBulkCopyobject.
|
|
|
The asynchronous version of WriteToServer, which copies all rows from
the supplied DbDataReaderarray to a destination table
specified by the DestinationTableName property of the SqlBulkCopyobject.
|
|
|
The asynchronous version of WriteToServer, which copies all rows in the
supplied IDataReader to a destination table specified by
the DestinationTableName property of the SqlBulkCopy object.
|
|
|
The asynchronous version of WriteToServer, which copies all rows in the
supplied IDataReader to a destination table specified by
the DestinationTableName property of the SqlBulkCopy object.
The cancellation token can be used to request that the
operation be abandoned before the command timeout elapses. Exceptions will be
reported via the returned Task object.
|
Events
Name
|
Description
|
|
|
Occurs every time that the number of rows specified by
the NotifyAfter property have been processed.
|
Explicit
Interface Implementations
Name
|
Description
|
|
|
Releases all resources used by the current instance of
the SqlBulkCopy class.
|
No comments:
Post a Comment