-
Notifications
You must be signed in to change notification settings - Fork 317
Optional ORDER hints for SqlBulkCopy #540
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
e431f08
order hint implementation
84ec1ab
minor changes
bef48f6
minor changes
51b37d8
Update Microsoft.Data.SqlClient.sln
johnnypham eb82141
modified tests
d214355
modified tests
b25e3c8
Tests now use DataTestUtility to drop tables. Added tests for WriteTo…
8834023
Merge branch 'bulkCopyOrderHints' of https://github.com/johnnypham/Sq…
5328079
assigned task number to order hint identity column test
d88cb03
Collection class reworked to handle duplicate columns
johnnypham d7f3deb
spacing
johnnypham 9e737cd
Updated tests for duplicate column names, updated documentation
johnnypham 4cd38d5
Added couple asserts
johnnypham 25f352f
Clean up tests
johnnypham 9900f72
Remove NameChanging event in ref
johnnypham b9416ee
Update doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopy.xml
9e4f02d
Update doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopyColumnOrderHi…
cf680d9
Update doc/snippets/Microsoft.Data.SqlClient/SqlBulkCopyColumnOrderHi…
968bd26
Addressing review comments
johnnypham de47b64
Merge branch 'bulkCopyOrderHints' of https://github.com/johnnypham/Sq…
johnnypham cb2eef3
spacing
johnnypham 2afb1a8
Added test for consecutive WriteToServer calls
johnnypham 9ed653d
Fixed test
johnnypham 18d5d7d
Merge branch 'master' into bulkCopyOrderHints
d9b4141
Merge branch 'master' into bulkCopyOrderHints
985662f
Update Microsoft.Data.SqlClient.ManualTesting.Tests.csproj
477913c
Update SqlBulkCopy_ColumnOrderHintCollectionClear.cs
5d9b9f5
Update SqlBulkCopyColumnOrderHint.xml
d5fe90d
Update SqlBulkCopyColumnOrderHintCollection.xml
9cde0c4
Addressing comments
johnnypham 0093a06
Merge branch 'bulkCopyOrderHints' of https://github.com/johnnypham/Sq…
johnnypham 2140fd7
Revert "Addressing comments"
johnnypham 465fa74
Revert "Merge branch 'bulkCopyOrderHints' of https://github.com/johnn…
johnnypham 242b1c3
Merge branch 'master' into bulkCopyOrderHints
johnnypham ecb43a7
Addressing comments
johnnypham 982a802
Addressing comments
johnnypham d5b8620
Addressing comments
johnnypham fd9183d
Update SqlBulkCopy.xml
d8a44b3
Addressing comments
johnnypham 3b8652f
Merge branch 'master' into bulkCopyOrderHints
johnnypham 8ceb9fb
XML comments
johnnypham 9c71b2f
Update SqlBulkCopyColumnOrderHintCollection.cs
johnnypham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | ||
|
|
||
| // 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;"; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.