SqlBulkCopyOptions 枚举
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定用于 SqlBulkCopy实例的一个或多个选项的按位标志。
此枚举支持其成员值的按位组合。
public enum class SqlBulkCopyOptions
[System.Flags]
public enum SqlBulkCopyOptions
[<System.Flags>]
type SqlBulkCopyOptions =
Public Enum SqlBulkCopyOptions
- 继承
- 属性
字段
| 名称 | 值 | 说明 |
|---|---|---|
| Default | 0 | 对所有选项使用默认值。 |
| KeepIdentity | 1 | 保留源标识值。 如果未指定,则标识值由目标分配。 |
| CheckConstraints | 2 | 在插入数据的同时检查约束。 默认情况下,不会检查约束。 |
| TableLock | 4 | 获取大容量复制操作期间的大容量更新锁定。 如果未指定,则使用行锁。 |
| KeepNulls | 8 | 无论默认值的设置为何,在目标表中都将保留 null 值。 如果未指定,则 null 值将替换为默认值(如果适用)。 |
| FireTriggers | 16 | 指定后,使服务器触发插入到数据库中的行的插入触发器。 |
| UseInternalTransaction | 32 | 指定后,每次批量复制操作都将在事务中发生。 如果指示此选项并向构造函数提供SqlTransaction对象,则会发生。ArgumentException |
| AllowEncryptedValueModifications | 64 | 指定后, AllowEncryptedValueModifications 允许在表或数据库之间批量复制加密数据,而无需解密数据。 通常,应用程序将从一个表中的加密列中选择数据并且无需密该数据(应用将连接到其列加密设置关键字设置为禁用的数据库),然后会使用此选项批量插入数据,这些数据仍然是加密的。 有关详细信息,请参阅 Always Encrypted。 如果指定 AllowEncryptedValueModifications ,因为这可能会导致数据库损坏,因为驱动程序不会检查数据是否确实已加密,或者是否使用与目标列相同的加密类型、算法和密钥进行正确加密。 |
示例
以下控制台应用程序演示如何执行大容量加载,将源表的标识列中的值复制到目标表中的相应列,而不是为每个行的标识列生成新值。
若要查看选项如何更改大容量加载的工作方式,请使用 dbo 运行示例。BulkCopyDemoMatchingColumns 表为空。 从源加载所有行。 接下来,再次运行示例,而不清空表。 引发异常,代码会将一条消息写入控制台窗口,通知你由于主键冲突而未添加行。
Important
除非已按批量复制示例设置中所述创建了工作表,否则此示例不会运行。 提供此代码是为了演示仅使用 SqlBulkCopy 时的语法。 如果源表和目标表位于同一 SQL Server 实例中,则使用 Transact-SQL INSERT … SELECT 语句复制数据更容易、更快。
using System.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();
// Create the SqlBulkCopy object using a connection string
// and the KeepIdentity option.
// In the real world you would not use SqlBulkCopy to move
// data from one table to the other in the same database.
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity))
{
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;";
}
}
Imports System.Data.SqlClient
Module Module1
Sub Main()
Dim connectionString As String = GetConnectionString()
' Open a connection to the AdventureWorks database.
Using sourceConnection As SqlConnection = _
New SqlConnection(connectionString)
sourceConnection.Open()
' Perform an initial count on the destination table.
Dim commandRowCount As New SqlCommand( _
"SELECT COUNT(*) FROM dbo.BulkCopyDemoMatchingColumns;", _
sourceConnection)
Dim countStart As Long = _
System.Convert.ToInt32(commandRowCount.ExecuteScalar())
Console.WriteLine("Starting row count = {0}", countStart)
' Get data from the source table as a SqlDataReader.
Dim commandSourceData As SqlCommand = New SqlCommand( _
"SELECT ProductID, Name, ProductNumber " & _
"FROM Production.Product;", sourceConnection)
Dim reader As SqlDataReader = commandSourceData.ExecuteReader
' Create the SqlBulkCopy object using a connection string
' and the KeepIdentity option.
' In the real world you would not use SqlBulkCopy to move
' data from one table to the other in the same database.
Using bulkCopy As SqlBulkCopy = _
New SqlBulkCopy(connectionString, SqlBulkCopyOptions.KeepIdentity)
bulkCopy.DestinationTableName = "dbo.BulkCopyDemoMatchingColumns"
Try
' Write from the source to the destination.
bulkCopy.WriteToServer(reader)
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
' Close the SqlDataReader. The SqlBulkCopy
' object is automatically closed at the end
' of the Using block.
reader.Close()
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 GetConnectionString() As String
' 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;"
End Function
End Module
注解
构造SqlBulkCopyOptions实例时,SqlBulkCopy可以使用枚举来更改WriteToServer该实例的方法的行为方式。