Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional ORDER hints for SqlBulkCopy #540

Merged
merged 42 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
e431f08
order hint implementation
Apr 24, 2020
84ec1ab
minor changes
Apr 24, 2020
bef48f6
minor changes
Apr 27, 2020
51b37d8
Update Microsoft.Data.SqlClient.sln
johnnypham Apr 27, 2020
eb82141
modified tests
Apr 27, 2020
d214355
modified tests
Apr 27, 2020
b25e3c8
Tests now use DataTestUtility to drop tables. Added tests for WriteTo…
Apr 28, 2020
8834023
Merge branch 'bulkCopyOrderHints' of https://github.com/johnnypham/Sq…
Apr 28, 2020
5328079
assigned task number to order hint identity column test
Apr 29, 2020
d88cb03
Collection class reworked to handle duplicate columns
johnnypham May 9, 2020
d7f3deb
spacing
johnnypham May 9, 2020
9e737cd
Updated tests for duplicate column names, updated documentation
johnnypham May 11, 2020
4cd38d5
Added couple asserts
johnnypham May 11, 2020
25f352f
Clean up tests
johnnypham May 18, 2020
9900f72
Remove NameChanging event in ref
johnnypham May 18, 2020
b9416ee
Update doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml
May 19, 2020
9e4f02d
Update doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopyColumnOrderHi…
May 19, 2020
cf680d9
Update doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopyColumnOrderHi…
May 19, 2020
968bd26
Addressing review comments
johnnypham May 19, 2020
de47b64
Merge branch 'bulkCopyOrderHints' of https://github.com/johnnypham/Sq…
johnnypham May 19, 2020
cb2eef3
spacing
johnnypham May 19, 2020
2afb1a8
Added test for consecutive WriteToServer calls
johnnypham May 19, 2020
9ed653d
Fixed test
johnnypham May 20, 2020
18d5d7d
Merge branch 'master' into bulkCopyOrderHints
May 20, 2020
d9b4141
Merge branch 'master' into bulkCopyOrderHints
May 22, 2020
985662f
Update Microsoft.Data.SqlClient.ManualTesting.Tests.csproj
May 22, 2020
477913c
Update SqlBulkCopy_ColumnOrderHintCollectionClear.cs
May 22, 2020
5d9b9f5
Update SqlBulkCopyColumnOrderHint.xml
May 22, 2020
d5fe90d
Update SqlBulkCopyColumnOrderHintCollection.xml
May 22, 2020
9cde0c4
Addressing comments
johnnypham May 29, 2020
0093a06
Merge branch 'bulkCopyOrderHints' of https://github.com/johnnypham/Sq…
johnnypham May 29, 2020
2140fd7
Revert "Addressing comments"
johnnypham May 29, 2020
465fa74
Revert "Merge branch 'bulkCopyOrderHints' of https://github.com/johnn…
johnnypham May 29, 2020
242b1c3
Merge branch 'master' into bulkCopyOrderHints
johnnypham May 29, 2020
ecb43a7
Addressing comments
johnnypham May 29, 2020
982a802
Addressing comments
johnnypham Jun 4, 2020
d5b8620
Addressing comments
johnnypham Jun 4, 2020
fd9183d
Update SqlBulkCopy.xml
Jun 5, 2020
d8a44b3
Addressing comments
johnnypham Jun 9, 2020
3b8652f
Merge branch 'master' into bulkCopyOrderHints
johnnypham Jun 9, 2020
8ceb9fb
XML comments
johnnypham Jun 10, 2020
9c71b2f
Update SqlBulkCopyColumnOrderHintCollection.cs
johnnypham Jun 10, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions doc/samples/SqlBulkCopy_ColumnOrderHint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// <Snippet1>
using System;
using System.Data;
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();

// Set up the bulk copy object.
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(connectionString))
{
bulkCopy.DestinationTableName =
"dbo.BulkCopyDemoMatchingColumns";

// Setup an order hint for the ProductNumber column.
SqlBulkCopyColumnOrderHint hintNumber =
new SqlBulkCopyColumnOrderHint("ProductNumber", SortOrder.Ascending);
bulkCopy.ColumnOrderHints.Add(hintNumber);

// Write from the source to the destination.
try
{
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;";
}
}
// </Snippet1>
83 changes: 83 additions & 0 deletions doc/samples/SqlBulkCopy_ColumnOrderHintCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// <Snippet1>
using System;
using System.Data;
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();

// Set up the bulk copy object.
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(connectionString))
{
bulkCopy.DestinationTableName =
"dbo.BulkCopyDemoMatchingColumns";

// Specify the sort order for the ProductNumber column in
// the destination table.
bulkCopy.ColumnOrderHints.Add("ProductNumber", SortOrder.Ascending);

// Write from the source to the destination.
try
{
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;";
}
}
// </Snippet1>
86 changes: 86 additions & 0 deletions doc/samples/SqlBulkCopy_ColumnOrderHintCollectionAdd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// <Snippet1>
using System;
using System.Data;
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();

// Set up the bulk copy object.
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(connectionString))
{
bulkCopy.DestinationTableName =
"dbo.BulkCopyDemoMatchingColumns";

// Specify the sort order for the ProductNumber column in
// the destination table.
// Setup an order hint for the ProductNumber column.
SqlBulkCopyColumnOrderHint hintNumber =
new SqlBulkCopyColumnOrderHint("ProductNumber", SortOrder.Ascending);
bulkCopy.ColumnOrderHints.Add(hintNumber);

// Write from the source to the destination.
try
{
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;";
}
}
// </Snippet1>
83 changes: 83 additions & 0 deletions doc/samples/SqlBulkCopy_ColumnOrderHintCollectionAdd2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// <Snippet1>
using System;
using System.Data;
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();

// Set up the bulk copy object.
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(connectionString))
{
bulkCopy.DestinationTableName =
"dbo.BulkCopyDemoMatchingColumns";

// Specify the sort order for the ProductNumber column in
// the destination table.
bulkCopy.ColumnOrderHints.Add("ProductNumber", SortOrder.Ascending);

// Write from the source to the destination.
try
{
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;";
}
}
// </Snippet1>
Loading