Language

SqlBulkCopy.DestinationTableName プロパティ

定義

サーバー上にあるコピー先のテーブルの名前。

public:
 property System::String ^ DestinationTableName { System::String ^ get(); void set(System::String ^ value); };
public string DestinationTableName { get; set; }
member this.DestinationTableName : string with get, set
Public Property DestinationTableName As String

プロパティ値

DestinationTableName プロパティの文字列値。または、値が指定されていない場合は null。

例

次のコンソール アプリケーションは、既に開いている接続を使用してデータを一括読み込みする方法を示しています。 変換先テーブルは、 AdventureWorks データベース内のテーブルです。

この例では、最初に接続を使用して、SQL Server テーブルからインスタンスにデータを読み取る必要がありますSqlDataReader。 ソース データは、SQL Serverに配置する必要はありません。に読み込んだり、 にIDataReader読み込んだりできる任意のデータ ソースをDataTable使用できます。

重要

このサンプルは、「バルク コピー サンプルのセットアップ」で説明されているように作業テーブルを作成してからでないと動作しません。 このコードでは、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;";
    }
}

注釈

が呼び出されたときに WriteToServer が設定されていない場合DestinationTableNameは、 ArgumentNullException がスローされます。 操作の実行中に WriteToServer が変更された場合DestinationTableName、変更は現在の操作には影響しません。 新しい DestinationTableName 値は、次にメソッドが WriteToServer 呼び出されるときに使用されます。

DestinationTableName は 3 部構成の名前 (<database>.<owningschema>.<name>) です。 必要に応じ、テーブル名をデータベースと所有しているスキーマで修飾することができます。 ただし、テーブル名でアンダースコア ("_") またはその他の特殊文字を使用する場合は、([<database>.<owningschema>.<name_01>]) のように周囲の角かっこを使用して名前をエスケープする必要があります。

プロパティの や tempdb.<owner>.#tableDestinationTableName などのtempdb..#table値を使用して、一時テーブルにデータを一括コピーできます。

適用対象