SqlBulkCopy 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
這樣可以有效地大量載入 SQL Server 資料表,從其他來源取得資料。
public ref class SqlBulkCopy sealed : IDisposable
public sealed class SqlBulkCopy : IDisposable
type SqlBulkCopy = class
interface IDisposable
Public NotInheritable Class SqlBulkCopy
Implements IDisposable
- 繼承
-
SqlBulkCopy
- 實作
範例
下列主控台應用程式示範如何使用 SqlBulkCopy 類別載入資料。 在這個範例中,SqlDataReader 用於從 SQL Server AdventureWorks 資料庫的 Production.Product 資料表,將資料複製到同一資料庫中的類似資料表中。
這很重要
除非您已如大量複製範例設定中所述建立工作資料表,否則將不會執行此範例。
這個程式碼僅是為了示範使用 SqlBulkCopy 的語法而提供。 如果來源和目的資料表在同一個 SQL Server 實例中,使用 Transact-SQL INSERT … SELECT 語句來複製資料會更簡單且快速。
using Microsoft.Data.SqlClient;
class Program
{
static void Main()
{
string connectionString = GetConnectionString();
// Open a sourceConnection to the AdventureWorks database.
using (SqlConnection sourceConnection =
new SqlConnection(connectionString))
{
sourceConnection.Open();
// Perform an initial count on the destination table.
SqlCommand commandRowCount = new SqlCommand(
"SELECT COUNT(*) FROM " +
"dbo.BulkCopyDemoMatchingColumns;",
sourceConnection);
long countStart = System.Convert.ToInt32(
commandRowCount.ExecuteScalar());
Console.WriteLine("Starting row count = {0}", countStart);
// Get data from the source table as a SqlDataReader.
SqlCommand commandSourceData = new SqlCommand(
"SELECT ProductID, Name, " +
"ProductNumber " +
"FROM Production.Product;", sourceConnection);
SqlDataReader reader =
commandSourceData.ExecuteReader();
// Open the destination connection. In the real world you would
// not use SqlBulkCopy to move data from one table to the other
// in the same database. This is for demonstration purposes only.
using (SqlConnection destinationConnection =
new SqlConnection(connectionString))
{
destinationConnection.Open();
// Set up the bulk copy object.
// Note that the column positions in the source
// data reader match the column positions in
// the destination table so there is no need to
// map columns.
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(destinationConnection))
{
bulkCopy.DestinationTableName =
"dbo.BulkCopyDemoMatchingColumns";
try
{
// Write from the source to the destination.
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
// Close the SqlDataReader. The SqlBulkCopy
// object is automatically closed at the end
// of the using block.
reader.Close();
}
}
// Perform a final count on the destination
// table to see how many rows were added.
long countEnd = 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();
}
}
}
private static string GetConnectionString()
// To avoid storing the sourceConnection string in your code,
// you can retrieve it from a configuration file.
{
return "Data Source=(local); " +
" Integrated Security=true;" +
"Initial Catalog=AdventureWorks;";
}
}
備註
Microsoft SQL Server 包含一個受歡迎的命令提示字元工具,名為 bcp,用於將資料從一個資料表移到另一個資料表,無論是在單一伺服器上,還是在伺服器間。 這門 SqlBulkCopy 課讓你可以寫出提供類似功能的託管程式碼解決方案。 還有其他方法可將資料載入 SQL Server 資料表 (例如 INSERT 陳述式) 中,但 SqlBulkCopy 提供顯著超越其他方法的效能優勢。 SqlBulkCopy 類別只能用來將資料寫入到 SQL Server 資料表。 然而,資料來源不限於SQL Server;只要資料能載入 DataTable 實例或以 IDataReader 實例讀取,任何資料來源皆可使用。 SqlBulkCopy 在將型別為 SqlDateTime 的 DataTable 欄位批量載入 2008 SQL Server 新增的日期/時間型別之一的 SQL Server 欄位時會失敗。
建構函式
| 名稱 | Description |
|---|---|
| SqlBulkCopy(SqlConnection, SqlBulkCopyOptions, SqlTransaction) |
使用提供的現有開放實例SqlConnection初始化該SqlBulkCopy類別的新實例。 該 SqlBulkCopy 實例依據參數中提供的 |
| SqlBulkCopy(SqlConnection) |
使用 SqlBulkCopy 的指定開放執行個體,以初始化 SqlConnection 類別的新執行個體。 |
| SqlBulkCopy(String, SqlBulkCopyOptions) |
根據所提供的 SqlConnection 來初始化並開啟 |
| SqlBulkCopy(String) |
根據所提供的 SqlConnection 來初始化並開啟 |
屬性
| 名稱 | Description |
|---|---|
| BatchSize |
每個批次中的資料列數。 在每個批次的結尾,會將該批次中的資料列傳送到伺服器。 |
| BulkCopyTimeout |
操作完成的時間數,超過超時。 |
| ColumnMappings |
歸還一組 SqlBulkCopyColumnMapping 物品。 在資料來源的資料行和目的地的資料行之間,資料行對應可定義其關聯性。 |
| ColumnOrderHints |
歸還一組 SqlBulkCopyColumnOrderHint 物品。 欄位順序提示描述目的資料表叢集索引中欄位的排序順序。 |
| DestinationTableName |
該伺服器的目的地資料表名稱。 |
| EnableStreaming |
啟用或停用 SqlBulkCopy 物件以串流從物件 IDataReader 的資料 |
| NotifyAfter |
定義在產生通知事件前需處理的列數。 |
| RowsCopied |
正在進行的批量複製操作中處理的列數。 |
| RowsCopied64 |
正在進行的批量複製操作中處理的列數。 |
方法
事件
| 名稱 | Description |
|---|---|
| SqlRowsCopied |
每當處理屬性指定的 NotifyAfter 列數時,都會發生。 |
明確介面實作
| 名稱 | Description |
|---|---|
| IDisposable.Dispose() |
釋放目前類別實例 SqlBulkCopy 所使用的所有資源。 |