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;您可以使用可讀取至 或載入 至 IDataReaderDataTable 的任何資料來源。

重要

除非您已建立如 大量複製範例安裝程式中所述的工作資料表,否則不會執行此範例。 這個程式碼僅是為了示範使用 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;";
    }
}

備註

如果在 DestinationTableName 呼叫 時 WriteToServer 尚未設定 , ArgumentNullException 則會擲回 。 如果在 DestinationTableName 作業執行時 WriteToServer 修改 ,則變更不會影響目前的作業。 下次呼叫方法時 WriteToServer ,會使用新的 DestinationTableName 值。

DestinationTableName 是三部分名稱 (<database>.<owningschema>.<name>)。 如果您要加以選擇,則可以用該資料表的資料庫和主控結構描述來限定該資料表名稱。 不過,如果資料表名稱使用底線 (「_」) 或任何其他特殊字元,您必須使用括弧逸出名稱,如 ([<database>.<owningschema>.<name_01>]) 。

您可以使用 屬性的值,將資料大量複製到臨時表,例如 tempdb..#tabletempdb.<owner>.#tableDestinationTableName

適用於