Skip to content

Commit 01939c3

Browse files
Merge branch 'master' into DeviceCodeFlow
2 parents ae41144 + 0d19970 commit 01939c3

File tree

72 files changed

+3987
-70
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+3987
-70
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// <Snippet1>
2+
using System;
3+
using System.Data;
4+
using Microsoft.Data.SqlClient;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
string connectionString = GetConnectionString();
11+
// Open a sourceConnection to the AdventureWorks database.
12+
using (SqlConnection sourceConnection =
13+
new SqlConnection(connectionString))
14+
{
15+
sourceConnection.Open();
16+
17+
// Perform an initial count on the destination table.
18+
SqlCommand commandRowCount = new SqlCommand(
19+
"SELECT COUNT(*) FROM " +
20+
"dbo.BulkCopyDemoMatchingColumns;",
21+
sourceConnection);
22+
long countStart = System.Convert.ToInt32(
23+
commandRowCount.ExecuteScalar());
24+
Console.WriteLine("Starting row count = {0}", countStart);
25+
26+
// Get data from the source table as a SqlDataReader.
27+
SqlCommand commandSourceData = new SqlCommand(
28+
"SELECT ProductID, Name, " +
29+
"ProductNumber " +
30+
"FROM Production.Product;", sourceConnection);
31+
SqlDataReader reader =
32+
commandSourceData.ExecuteReader();
33+
34+
// Set up the bulk copy object.
35+
using (SqlBulkCopy bulkCopy =
36+
new SqlBulkCopy(connectionString))
37+
{
38+
bulkCopy.DestinationTableName =
39+
"dbo.BulkCopyDemoMatchingColumns";
40+
41+
// Setup an order hint for the ProductNumber column.
42+
SqlBulkCopyColumnOrderHint hintNumber =
43+
new SqlBulkCopyColumnOrderHint("ProductNumber", SortOrder.Ascending);
44+
bulkCopy.ColumnOrderHints.Add(hintNumber);
45+
46+
// Write from the source to the destination.
47+
try
48+
{
49+
bulkCopy.WriteToServer(reader);
50+
}
51+
catch (Exception ex)
52+
{
53+
Console.WriteLine(ex.Message);
54+
}
55+
finally
56+
{
57+
// Close the SqlDataReader. The SqlBulkCopy
58+
// object is automatically closed at the end
59+
// of the using block.
60+
reader.Close();
61+
}
62+
}
63+
64+
// Perform a final count on the destination
65+
// table to see how many rows were added.
66+
long countEnd = System.Convert.ToInt32(
67+
commandRowCount.ExecuteScalar());
68+
Console.WriteLine("Ending row count = {0}", countEnd);
69+
Console.WriteLine("{0} rows were added.", countEnd - countStart);
70+
Console.WriteLine("Press Enter to finish.");
71+
Console.ReadLine();
72+
}
73+
}
74+
75+
private static string GetConnectionString()
76+
// To avoid storing the sourceConnection string in your code,
77+
// you can retrieve it from a configuration file.
78+
{
79+
return "Data Source=(local); " +
80+
" Integrated Security=true;" +
81+
"Initial Catalog=AdventureWorks;";
82+
}
83+
}
84+
// </Snippet1>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// <Snippet1>
2+
using System;
3+
using System.Data;
4+
using Microsoft.Data.SqlClient;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
string connectionString = GetConnectionString();
11+
// Open a sourceConnection to the AdventureWorks database.
12+
using (SqlConnection sourceConnection =
13+
new SqlConnection(connectionString))
14+
{
15+
sourceConnection.Open();
16+
17+
// Perform an initial count on the destination table.
18+
SqlCommand commandRowCount = new SqlCommand(
19+
"SELECT COUNT(*) FROM " +
20+
"dbo.BulkCopyDemoMatchingColumns;",
21+
sourceConnection);
22+
long countStart = System.Convert.ToInt32(
23+
commandRowCount.ExecuteScalar());
24+
Console.WriteLine("Starting row count = {0}", countStart);
25+
26+
// Get data from the source table as a SqlDataReader.
27+
SqlCommand commandSourceData = new SqlCommand(
28+
"SELECT ProductID, Name, " +
29+
"ProductNumber " +
30+
"FROM Production.Product;", sourceConnection);
31+
SqlDataReader reader =
32+
commandSourceData.ExecuteReader();
33+
34+
// Set up the bulk copy object.
35+
using (SqlBulkCopy bulkCopy =
36+
new SqlBulkCopy(connectionString))
37+
{
38+
bulkCopy.DestinationTableName =
39+
"dbo.BulkCopyDemoMatchingColumns";
40+
41+
// Specify the sort order for the ProductNumber column in
42+
// the destination table.
43+
bulkCopy.ColumnOrderHints.Add("ProductNumber", SortOrder.Ascending);
44+
45+
// Write from the source to the destination.
46+
try
47+
{
48+
bulkCopy.WriteToServer(reader);
49+
}
50+
catch (Exception ex)
51+
{
52+
Console.WriteLine(ex.Message);
53+
}
54+
finally
55+
{
56+
// Close the SqlDataReader. The SqlBulkCopy
57+
// object is automatically closed at the end
58+
// of the using block.
59+
reader.Close();
60+
}
61+
}
62+
63+
// Perform a final count on the destination
64+
// table to see how many rows were added.
65+
long countEnd = System.Convert.ToInt32(
66+
commandRowCount.ExecuteScalar());
67+
Console.WriteLine("Ending row count = {0}", countEnd);
68+
Console.WriteLine("{0} rows were added.", countEnd - countStart);
69+
Console.WriteLine("Press Enter to finish.");
70+
Console.ReadLine();
71+
}
72+
}
73+
74+
private static string GetConnectionString()
75+
// To avoid storing the sourceConnection string in your code,
76+
// you can retrieve it from a configuration file.
77+
{
78+
return "Data Source=(local); " +
79+
" Integrated Security=true;" +
80+
"Initial Catalog=AdventureWorks;";
81+
}
82+
}
83+
// </Snippet1>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// <Snippet1>
2+
using System;
3+
using System.Data;
4+
using Microsoft.Data.SqlClient;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
string connectionString = GetConnectionString();
11+
// Open a sourceConnection to the AdventureWorks database.
12+
using (SqlConnection sourceConnection =
13+
new SqlConnection(connectionString))
14+
{
15+
sourceConnection.Open();
16+
17+
// Perform an initial count on the destination table.
18+
SqlCommand commandRowCount = new SqlCommand(
19+
"SELECT COUNT(*) FROM " +
20+
"dbo.BulkCopyDemoMatchingColumns;",
21+
sourceConnection);
22+
long countStart = System.Convert.ToInt32(
23+
commandRowCount.ExecuteScalar());
24+
Console.WriteLine("Starting row count = {0}", countStart);
25+
26+
// Get data from the source table as a SqlDataReader.
27+
SqlCommand commandSourceData = new SqlCommand(
28+
"SELECT ProductID, Name, " +
29+
"ProductNumber " +
30+
"FROM Production.Product;", sourceConnection);
31+
SqlDataReader reader =
32+
commandSourceData.ExecuteReader();
33+
34+
// Set up the bulk copy object.
35+
using (SqlBulkCopy bulkCopy =
36+
new SqlBulkCopy(connectionString))
37+
{
38+
bulkCopy.DestinationTableName =
39+
"dbo.BulkCopyDemoMatchingColumns";
40+
41+
// Specify the sort order for the ProductNumber column in
42+
// the destination table.
43+
// Setup an order hint for the ProductNumber column.
44+
SqlBulkCopyColumnOrderHint hintNumber =
45+
new SqlBulkCopyColumnOrderHint("ProductNumber", SortOrder.Ascending);
46+
bulkCopy.ColumnOrderHints.Add(hintNumber);
47+
48+
// Write from the source to the destination.
49+
try
50+
{
51+
bulkCopy.WriteToServer(reader);
52+
}
53+
catch (Exception ex)
54+
{
55+
Console.WriteLine(ex.Message);
56+
}
57+
finally
58+
{
59+
// Close the SqlDataReader. The SqlBulkCopy
60+
// object is automatically closed at the end
61+
// of the using block.
62+
reader.Close();
63+
}
64+
}
65+
66+
// Perform a final count on the destination
67+
// table to see how many rows were added.
68+
long countEnd = System.Convert.ToInt32(
69+
commandRowCount.ExecuteScalar());
70+
Console.WriteLine("Ending row count = {0}", countEnd);
71+
Console.WriteLine("{0} rows were added.", countEnd - countStart);
72+
Console.WriteLine("Press Enter to finish.");
73+
Console.ReadLine();
74+
}
75+
}
76+
77+
private static string GetConnectionString()
78+
// To avoid storing the sourceConnection string in your code,
79+
// you can retrieve it from a configuration file.
80+
{
81+
return "Data Source=(local); " +
82+
" Integrated Security=true;" +
83+
"Initial Catalog=AdventureWorks;";
84+
}
85+
}
86+
// </Snippet1>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// <Snippet1>
2+
using System;
3+
using System.Data;
4+
using Microsoft.Data.SqlClient;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
string connectionString = GetConnectionString();
11+
// Open a sourceConnection to the AdventureWorks database.
12+
using (SqlConnection sourceConnection =
13+
new SqlConnection(connectionString))
14+
{
15+
sourceConnection.Open();
16+
17+
// Perform an initial count on the destination table.
18+
SqlCommand commandRowCount = new SqlCommand(
19+
"SELECT COUNT(*) FROM " +
20+
"dbo.BulkCopyDemoMatchingColumns;",
21+
sourceConnection);
22+
long countStart = System.Convert.ToInt32(
23+
commandRowCount.ExecuteScalar());
24+
Console.WriteLine("Starting row count = {0}", countStart);
25+
26+
// Get data from the source table as a SqlDataReader.
27+
SqlCommand commandSourceData = new SqlCommand(
28+
"SELECT ProductID, Name, " +
29+
"ProductNumber " +
30+
"FROM Production.Product;", sourceConnection);
31+
SqlDataReader reader =
32+
commandSourceData.ExecuteReader();
33+
34+
// Set up the bulk copy object.
35+
using (SqlBulkCopy bulkCopy =
36+
new SqlBulkCopy(connectionString))
37+
{
38+
bulkCopy.DestinationTableName =
39+
"dbo.BulkCopyDemoMatchingColumns";
40+
41+
// Specify the sort order for the ProductNumber column in
42+
// the destination table.
43+
bulkCopy.ColumnOrderHints.Add("ProductNumber", SortOrder.Ascending);
44+
45+
// Write from the source to the destination.
46+
try
47+
{
48+
bulkCopy.WriteToServer(reader);
49+
}
50+
catch (Exception ex)
51+
{
52+
Console.WriteLine(ex.Message);
53+
}
54+
finally
55+
{
56+
// Close the SqlDataReader. The SqlBulkCopy
57+
// object is automatically closed at the end
58+
// of the using block.
59+
reader.Close();
60+
}
61+
}
62+
63+
// Perform a final count on the destination
64+
// table to see how many rows were added.
65+
long countEnd = System.Convert.ToInt32(
66+
commandRowCount.ExecuteScalar());
67+
Console.WriteLine("Ending row count = {0}", countEnd);
68+
Console.WriteLine("{0} rows were added.", countEnd - countStart);
69+
Console.WriteLine("Press Enter to finish.");
70+
Console.ReadLine();
71+
}
72+
}
73+
74+
private static string GetConnectionString()
75+
// To avoid storing the sourceConnection string in your code,
76+
// you can retrieve it from a configuration file.
77+
{
78+
return "Data Source=(local); " +
79+
" Integrated Security=true;" +
80+
"Initial Catalog=AdventureWorks;";
81+
}
82+
}
83+
// </Snippet1>

0 commit comments

Comments
 (0)