using System;
using System.Data.SqlClient;
class Program
{
static void Main()
{
string connectionString = "Data Source=(local);Integrated Security=true;Initial Catalog=AdventureWorks;";
using (SqlConnection sourceConnection = new SqlConnection(connectionString))
{
sourceConnection.Open();
SqlCommand commandRowCount = new SqlCommand("SELECT COUNT(*) FROM dbo.BulkCopyDemoMatchingColumns;",sourceConnection);
long countStart = System.Convert.ToInt32(commandRowCount.ExecuteScalar());
Console.WriteLine("Starting row count = {0}", countStart);
SqlCommand commandSourceData = new SqlCommand("SELECT ProductID,Name,ProductNumber FROM Production.Product;", sourceConnection);
SqlDataReader reader = commandSourceData.ExecuteReader();
using (SqlConnection destinationConnection = new SqlConnection(connectionString))
{
destinationConnection.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection))
{
bulkCopy.DestinationTableName = "dbo.BulkCopyDemoMatchingColumns";
try
{
bulkCopy.WriteToServer(reader);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
reader.Close();
}
}
long countEnd = System.Convert.ToInt32(commandRowCount.ExecuteScalar());
Console.WriteLine(countEnd);
}
}
}
}
|