diff --git a/doc/samples/SqlBulkCopy_ColumnMappingDestinationColumn.cs b/doc/samples/SqlBulkCopy_ColumnMappingDestinationColumn.cs
new file mode 100644
index 0000000000..d46a58a261
--- /dev/null
+++ b/doc/samples/SqlBulkCopy_ColumnMappingDestinationColumn.cs
@@ -0,0 +1,95 @@
+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.BulkCopyDemoDifferentColumns;",
+ 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.BulkCopyDemoDifferentColumns";
+
+ // Set up the column mappings source and destination.
+ SqlBulkCopyColumnMapping mapID = new SqlBulkCopyColumnMapping();
+ mapID.SourceColumn = "ProductID";
+ mapID.DestinationColumn = "ProdID";
+ bulkCopy.ColumnMappings.Add(mapID);
+
+ SqlBulkCopyColumnMapping mapName = new SqlBulkCopyColumnMapping();
+ mapName.SourceColumn = "Name";
+ mapName.DestinationColumn = "ProdName";
+ bulkCopy.ColumnMappings.Add(mapName);
+
+ SqlBulkCopyColumnMapping mapNumber = new SqlBulkCopyColumnMapping();
+ mapNumber.SourceColumn = "ProductNumber";
+ mapNumber.DestinationColumn = "ProdNum";
+ bulkCopy.ColumnMappings.Add(mapNumber);
+
+ // 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;";
+ }
+}
+//
diff --git a/doc/samples/SqlBulkCopy_ColumnMappingDestinationOrdinal.cs b/doc/samples/SqlBulkCopy_ColumnMappingDestinationOrdinal.cs
new file mode 100644
index 0000000000..19e31f282d
--- /dev/null
+++ b/doc/samples/SqlBulkCopy_ColumnMappingDestinationOrdinal.cs
@@ -0,0 +1,95 @@
+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.BulkCopyDemoDifferentColumns;",
+ 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.BulkCopyDemoDifferentColumns";
+
+ // Set up the column mappings source and destination.
+ SqlBulkCopyColumnMapping mapID = new SqlBulkCopyColumnMapping();
+ mapID.SourceOrdinal = 0;
+ mapID.DestinationOrdinal = 0;
+ bulkCopy.ColumnMappings.Add(mapID);
+
+ SqlBulkCopyColumnMapping mapName = new SqlBulkCopyColumnMapping();
+ mapName.SourceOrdinal = 1;
+ mapName.DestinationOrdinal = 2;
+ bulkCopy.ColumnMappings.Add(mapName);
+
+ SqlBulkCopyColumnMapping mapNumber = new SqlBulkCopyColumnMapping();
+ mapNumber.SourceOrdinal = 2;
+ mapNumber.DestinationOrdinal = 1;
+ bulkCopy.ColumnMappings.Add(mapNumber);
+
+ // 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;";
+ }
+}
+//
diff --git a/doc/samples/SqlBulkCopy_ColumnMappingNameOrdinal.cs b/doc/samples/SqlBulkCopy_ColumnMappingNameOrdinal.cs
new file mode 100644
index 0000000000..bdda864974
--- /dev/null
+++ b/doc/samples/SqlBulkCopy_ColumnMappingNameOrdinal.cs
@@ -0,0 +1,92 @@
+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.BulkCopyDemoDifferentColumns;",
+ 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.BulkCopyDemoDifferentColumns";
+
+ // Set up the column mappings by name and ordinal.
+ SqlBulkCopyColumnMapping columnMapID =
+ new SqlBulkCopyColumnMapping("ProductID", 0);
+ bulkCopy.ColumnMappings.Add(columnMapID);
+
+ SqlBulkCopyColumnMapping columnMapName =
+ new SqlBulkCopyColumnMapping("Name", 2);
+ bulkCopy.ColumnMappings.Add(columnMapName);
+
+ SqlBulkCopyColumnMapping columnMapNumber =
+ new SqlBulkCopyColumnMapping("ProductNumber", 1);
+ bulkCopy.ColumnMappings.Add(columnMapNumber);
+
+ // 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;";
+ }
+}
+//
diff --git a/doc/samples/SqlBulkCopy_ColumnMappingOrdinal.cs b/doc/samples/SqlBulkCopy_ColumnMappingOrdinal.cs
new file mode 100644
index 0000000000..671383133f
--- /dev/null
+++ b/doc/samples/SqlBulkCopy_ColumnMappingOrdinal.cs
@@ -0,0 +1,92 @@
+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.BulkCopyDemoDifferentColumns;",
+ 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.BulkCopyDemoDifferentColumns";
+
+ // Set up the column mappings by ordinal.
+ SqlBulkCopyColumnMapping columnMapID =
+ new SqlBulkCopyColumnMapping(0, 0);
+ bulkCopy.ColumnMappings.Add(columnMapID);
+
+ SqlBulkCopyColumnMapping columnMapName =
+ new SqlBulkCopyColumnMapping(1, 2);
+ bulkCopy.ColumnMappings.Add(columnMapName);
+
+ SqlBulkCopyColumnMapping columnMapNumber =
+ new SqlBulkCopyColumnMapping(2, 1);
+ bulkCopy.ColumnMappings.Add(columnMapNumber);
+
+ // 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;";
+ }
+}
+//
diff --git a/doc/samples/SqlBulkCopy_ColumnMappingOrdinalName.cs b/doc/samples/SqlBulkCopy_ColumnMappingOrdinalName.cs
new file mode 100644
index 0000000000..8ca91ee00e
--- /dev/null
+++ b/doc/samples/SqlBulkCopy_ColumnMappingOrdinalName.cs
@@ -0,0 +1,92 @@
+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.BulkCopyDemoDifferentColumns;",
+ 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.BulkCopyDemoDifferentColumns";
+
+ // Set up the column mappings by ordinal and name.
+ SqlBulkCopyColumnMapping columnMapID =
+ new SqlBulkCopyColumnMapping(0, "ProdID");
+ bulkCopy.ColumnMappings.Add(columnMapID);
+
+ SqlBulkCopyColumnMapping columnMapName =
+ new SqlBulkCopyColumnMapping(1, "ProdName");
+ bulkCopy.ColumnMappings.Add(columnMapName);
+
+ SqlBulkCopyColumnMapping columnMapNumber =
+ new SqlBulkCopyColumnMapping(2, "ProdNum");
+ bulkCopy.ColumnMappings.Add(columnMapNumber);
+
+ // 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;";
+ }
+}
+//
diff --git a/doc/samples/SqlBulkCopy_ConnectionString.cs b/doc/samples/SqlBulkCopy_ConnectionString.cs
new file mode 100644
index 0000000000..50c3101369
--- /dev/null
+++ b/doc/samples/SqlBulkCopy_ConnectionString.cs
@@ -0,0 +1,81 @@
+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 a connection string.
+ // In the real world you would not use SqlBulkCopy to move
+ // data from one table to the other in the same database.
+ using (SqlBulkCopy bulkCopy =
+ new SqlBulkCopy(connectionString))
+ {
+ 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;";
+ }
+}
+//
diff --git a/doc/samples/SqlBulkCopy_DataRowState.cs b/doc/samples/SqlBulkCopy_DataRowState.cs
new file mode 100644
index 0000000000..6e36e6a3e1
--- /dev/null
+++ b/doc/samples/SqlBulkCopy_DataRowState.cs
@@ -0,0 +1,122 @@
+using System;
+using System.Data;
+//
+using Microsoft.Data.SqlClient;
+
+class Program
+{
+ static void Main()
+ {
+ string connectionString = GetConnectionString();
+ // Open a connection to the AdventureWorks database.
+ using (SqlConnection connection =
+ new SqlConnection(connectionString))
+ {
+ connection.Open();
+
+ // Perform an initial count on the destination table.
+ SqlCommand commandRowCount = new SqlCommand(
+ "SELECT COUNT(*) FROM " +
+ "dbo.BulkCopyDemoMatchingColumns;",
+ connection);
+ long countStart = System.Convert.ToInt32(
+ commandRowCount.ExecuteScalar());
+ Console.WriteLine("Starting row count = {0}", countStart);
+
+ // Create a table with some rows.
+ DataTable newProducts = MakeTable();
+
+ // Make a change to one of the rows in the DataTable.
+ DataRow row = newProducts.Rows[0];
+ row.BeginEdit();
+ row["Name"] = "AAA";
+ row.EndEdit();
+
+ // Create the SqlBulkCopy object.
+ // Note that the column positions in the source DataTable
+ // match the column positions in the destination table so
+ // there is no need to map columns.
+ using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
+ {
+ bulkCopy.DestinationTableName =
+ "dbo.BulkCopyDemoMatchingColumns";
+
+ try
+ {
+ // Write unchanged rows from the source to the destination.
+ bulkCopy.WriteToServer(newProducts, DataRowState.Unchanged);
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex.Message);
+ }
+ }
+
+ // 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 DataTable MakeTable()
+ // Create a new DataTable named NewProducts.
+ {
+ DataTable newProducts = new DataTable("NewProducts");
+
+ // Add three column objects to the table.
+ DataColumn productID = new DataColumn();
+ productID.DataType = System.Type.GetType("System.Int32");
+ productID.ColumnName = "ProductID";
+ productID.AutoIncrement = true;
+ newProducts.Columns.Add(productID);
+
+ DataColumn productName = new DataColumn();
+ productName.DataType = System.Type.GetType("System.String");
+ productName.ColumnName = "Name";
+ newProducts.Columns.Add(productName);
+
+ DataColumn productNumber = new DataColumn();
+ productNumber.DataType = System.Type.GetType("System.String");
+ productNumber.ColumnName = "ProductNumber";
+ newProducts.Columns.Add(productNumber);
+
+ // Create an array for DataColumn objects.
+ DataColumn[] keys = new DataColumn[1];
+ keys[0] = productID;
+ newProducts.PrimaryKey = keys;
+
+ // Add some new rows to the collection.
+ DataRow row = newProducts.NewRow();
+ row["Name"] = "CC-101-WH";
+ row["ProductNumber"] = "Cyclocomputer - White";
+
+ newProducts.Rows.Add(row);
+ row = newProducts.NewRow();
+ row["Name"] = "CC-101-BK";
+ row["ProductNumber"] = "Cyclocomputer - Black";
+
+ newProducts.Rows.Add(row);
+ row = newProducts.NewRow();
+ row["Name"] = "CC-101-ST";
+ row["ProductNumber"] = "Cyclocomputer - Stainless";
+ newProducts.Rows.Add(row);
+ newProducts.AcceptChanges();
+
+ // Return the new DataTable.
+ return newProducts;
+ }
+ private static string GetConnectionString()
+ // To avoid storing the connection string in your code,
+ // you can retrieve it from a configuration file.
+ {
+ return "Data Source=(local); " +
+ " Integrated Security=true;" +
+ "Initial Catalog=AdventureWorks;";
+ }
+}
+//
diff --git a/doc/samples/SqlBulkCopy_DataTable.cs b/doc/samples/SqlBulkCopy_DataTable.cs
new file mode 100644
index 0000000000..a3c8c98fae
--- /dev/null
+++ b/doc/samples/SqlBulkCopy_DataTable.cs
@@ -0,0 +1,116 @@
+using System;
+using System.Data;
+//
+using Microsoft.Data.SqlClient;
+
+class Program
+{
+ static void Main()
+ {
+ string connectionString = GetConnectionString();
+ // Open a connection to the AdventureWorks database.
+ using (SqlConnection connection =
+ new SqlConnection(connectionString))
+ {
+ connection.Open();
+
+ // Perform an initial count on the destination table.
+ SqlCommand commandRowCount = new SqlCommand(
+ "SELECT COUNT(*) FROM " +
+ "dbo.BulkCopyDemoMatchingColumns;",
+ connection);
+ long countStart = System.Convert.ToInt32(
+ commandRowCount.ExecuteScalar());
+ Console.WriteLine("Starting row count = {0}", countStart);
+
+ // Create a table with some rows.
+ DataTable newProducts = MakeTable();
+
+ // Create the SqlBulkCopy object.
+ // Note that the column positions in the source DataTable
+ // match the column positions in the destination table so
+ // there is no need to map columns.
+ using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection))
+ {
+ bulkCopy.DestinationTableName =
+ "dbo.BulkCopyDemoMatchingColumns";
+
+ try
+ {
+ // Write from the source to the destination.
+ bulkCopy.WriteToServer(newProducts);
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex.Message);
+ }
+ }
+
+ // 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 DataTable MakeTable()
+ // Create a new DataTable named NewProducts.
+ {
+ DataTable newProducts = new DataTable("NewProducts");
+
+ // Add three column objects to the table.
+ DataColumn productID = new DataColumn();
+ productID.DataType = System.Type.GetType("System.Int32");
+ productID.ColumnName = "ProductID";
+ productID.AutoIncrement = true;
+ newProducts.Columns.Add(productID);
+
+ DataColumn productName = new DataColumn();
+ productName.DataType = System.Type.GetType("System.String");
+ productName.ColumnName = "Name";
+ newProducts.Columns.Add(productName);
+
+ DataColumn productNumber = new DataColumn();
+ productNumber.DataType = System.Type.GetType("System.String");
+ productNumber.ColumnName = "ProductNumber";
+ newProducts.Columns.Add(productNumber);
+
+ // Create an array for DataColumn objects.
+ DataColumn[] keys = new DataColumn[1];
+ keys[0] = productID;
+ newProducts.PrimaryKey = keys;
+
+ // Add some new rows to the collection.
+ DataRow row = newProducts.NewRow();
+ row["Name"] = "CC-101-WH";
+ row["ProductNumber"] = "Cyclocomputer - White";
+
+ newProducts.Rows.Add(row);
+ row = newProducts.NewRow();
+ row["Name"] = "CC-101-BK";
+ row["ProductNumber"] = "Cyclocomputer - Black";
+
+ newProducts.Rows.Add(row);
+ row = newProducts.NewRow();
+ row["Name"] = "CC-101-ST";
+ row["ProductNumber"] = "Cyclocomputer - Stainless";
+ newProducts.Rows.Add(row);
+ newProducts.AcceptChanges();
+
+ // Return the new DataTable.
+ return newProducts;
+ }
+ private static string GetConnectionString()
+ // To avoid storing the connection string in your code,
+ // you can retrieve it from a configuration file.
+ {
+ return "Data Source=(local); " +
+ " Integrated Security=true;" +
+ "Initial Catalog=AdventureWorks;";
+ }
+}
+//
diff --git a/doc/samples/SqlBulkCopy_NotifyAfter.cs b/doc/samples/SqlBulkCopy_NotifyAfter.cs
new file mode 100644
index 0000000000..bf64c6f69c
--- /dev/null
+++ b/doc/samples/SqlBulkCopy_NotifyAfter.cs
@@ -0,0 +1,91 @@
+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("NotifyAfter Sample");
+ 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();
+
+ // Create the SqlBulkCopy object using a connection string.
+ // In the real world you would not use SqlBulkCopy to move
+ // data from one table to the other in the same database.
+ using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString))
+ {
+ bulkCopy.DestinationTableName =
+ "dbo.BulkCopyDemoMatchingColumns";
+
+ // Set up the event handler to notify after 50 rows.
+ bulkCopy.SqlRowsCopied +=
+ new SqlRowsCopiedEventHandler(OnSqlRowsCopied);
+ bulkCopy.NotifyAfter = 50;
+
+ 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 void OnSqlRowsCopied(
+ object sender, SqlRowsCopiedEventArgs e)
+ {
+ Console.WriteLine("Copied {0} so far...", e.RowsCopied);
+ }
+ 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;";
+ }
+}
+//
diff --git a/doc/samples/SqlBulkCopy_OrdersDetails.cs b/doc/samples/SqlBulkCopy_OrdersDetails.cs
new file mode 100644
index 0000000000..9fd713a548
--- /dev/null
+++ b/doc/samples/SqlBulkCopy_OrdersDetails.cs
@@ -0,0 +1,154 @@
+using System;
+using System.Data;
+//
+using Microsoft.Data.SqlClient;
+
+class Program
+{
+ static void Main()
+ {
+ string connectionString = GetConnectionString();
+ // Open a connection to the AdventureWorks database.
+ using (SqlConnection connection =
+ new SqlConnection(connectionString))
+ {
+ connection.Open();
+
+ // Empty the destination tables.
+ SqlCommand deleteHeader = new SqlCommand(
+ "DELETE FROM dbo.BulkCopyDemoOrderHeader;",
+ connection);
+ deleteHeader.ExecuteNonQuery();
+ SqlCommand deleteDetail = new SqlCommand(
+ "DELETE FROM dbo.BulkCopyDemoOrderDetail;",
+ connection);
+ deleteDetail.ExecuteNonQuery();
+
+ // Perform an initial count on the destination
+ // table with matching columns.
+ SqlCommand countRowHeader = new SqlCommand(
+ "SELECT COUNT(*) FROM dbo.BulkCopyDemoOrderHeader;",
+ connection);
+ long countStartHeader = System.Convert.ToInt32(
+ countRowHeader.ExecuteScalar());
+ Console.WriteLine(
+ "Starting row count for Header table = {0}",
+ countStartHeader);
+
+ // Perform an initial count on the destination
+ // table with different column positions.
+ SqlCommand countRowDetail = new SqlCommand(
+ "SELECT COUNT(*) FROM dbo.BulkCopyDemoOrderDetail;",
+ connection);
+ long countStartDetail = System.Convert.ToInt32(
+ countRowDetail.ExecuteScalar());
+ Console.WriteLine(
+ "Starting row count for Detail table = {0}",
+ countStartDetail);
+
+ // Get data from the source table as a SqlDataReader.
+ // The Sales.SalesOrderHeader and Sales.SalesOrderDetail
+ // tables are quite large and could easily cause a timeout
+ // if all data from the tables is added to the destination.
+ // To keep the example simple and quick, a parameter is
+ // used to select only orders for a particular account
+ // as the source for the bulk insert.
+ SqlCommand headerData = new SqlCommand(
+ "SELECT [SalesOrderID], [OrderDate], " +
+ "[AccountNumber] FROM [Sales].[SalesOrderHeader] " +
+ "WHERE [AccountNumber] = @accountNumber;",
+ connection);
+ SqlParameter parameterAccount = new SqlParameter();
+ parameterAccount.ParameterName = "@accountNumber";
+ parameterAccount.SqlDbType = SqlDbType.NVarChar;
+ parameterAccount.Direction = ParameterDirection.Input;
+ parameterAccount.Value = "10-4020-000034";
+ headerData.Parameters.Add(parameterAccount);
+ SqlDataReader readerHeader = headerData.ExecuteReader();
+
+ // Get the Detail data in a separate connection.
+ using (SqlConnection connection2 = new SqlConnection(connectionString))
+ {
+ connection2.Open();
+ SqlCommand sourceDetailData = new SqlCommand(
+ "SELECT [Sales].[SalesOrderDetail].[SalesOrderID], [SalesOrderDetailID], " +
+ "[OrderQty], [ProductID], [UnitPrice] FROM [Sales].[SalesOrderDetail] " +
+ "INNER JOIN [Sales].[SalesOrderHeader] ON [Sales].[SalesOrderDetail]." +
+ "[SalesOrderID] = [Sales].[SalesOrderHeader].[SalesOrderID] " +
+ "WHERE [AccountNumber] = @accountNumber;", connection2);
+
+ SqlParameter accountDetail = new SqlParameter();
+ accountDetail.ParameterName = "@accountNumber";
+ accountDetail.SqlDbType = SqlDbType.NVarChar;
+ accountDetail.Direction = ParameterDirection.Input;
+ accountDetail.Value = "10-4020-000034";
+ sourceDetailData.Parameters.Add(accountDetail);
+ SqlDataReader readerDetail = sourceDetailData.ExecuteReader();
+
+ // Create the SqlBulkCopy object.
+ using (SqlBulkCopy bulkCopy =
+ new SqlBulkCopy(connectionString))
+ {
+ bulkCopy.DestinationTableName =
+ "dbo.BulkCopyDemoOrderHeader";
+
+ // Write readerHeader to the destination.
+ try
+ {
+ bulkCopy.WriteToServer(readerHeader);
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex.Message);
+ }
+ finally
+ {
+ readerHeader.Close();
+ }
+
+ // Set up a different destination and
+ // map columns.
+ bulkCopy.DestinationTableName =
+ "dbo.BulkCopyDemoOrderDetail";
+
+ // Write readerDetail to the destination.
+ try
+ {
+ bulkCopy.WriteToServer(readerDetail);
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex.Message);
+ }
+ finally
+ {
+ readerDetail.Close();
+ }
+ }
+
+ // Perform a final count on the destination
+ // tables to see how many rows were added.
+ long countEndHeader = System.Convert.ToInt32(
+ countRowHeader.ExecuteScalar());
+ Console.WriteLine("{0} rows were added to the Header table.",
+ countEndHeader - countStartHeader);
+ long countEndDetail = System.Convert.ToInt32(
+ countRowDetail.ExecuteScalar());
+ Console.WriteLine("{0} rows were added to the Detail table.",
+ countEndDetail - countStartDetail);
+ Console.WriteLine("Press Enter to finish.");
+ Console.ReadLine();
+ }
+ }
+ }
+
+ private static string GetConnectionString()
+ // To avoid storing the connection string in your code,
+ // you can retrieve it from a configuration file.
+ {
+ return "Data Source=(local); " +
+ " Integrated Security=true;" +
+ "Initial Catalog=AdventureWorks;";
+ }
+}
+//
diff --git a/doc/samples/SqlBulkCopy_Timeout.cs b/doc/samples/SqlBulkCopy_Timeout.cs
new file mode 100644
index 0000000000..b684e6879d
--- /dev/null
+++ b/doc/samples/SqlBulkCopy_Timeout.cs
@@ -0,0 +1,84 @@
+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();
+
+ // Create the SqlBulkCopy object using a connection string.
+ // In the real world you would not use SqlBulkCopy to move
+ // data from one table to the other in the same database.
+ using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString))
+ {
+ bulkCopy.DestinationTableName =
+ "dbo.BulkCopyDemoMatchingColumns";
+
+ // Set the timeout.
+ bulkCopy.BulkCopyTimeout = 60;
+
+ 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;";
+ }
+}
+//
diff --git a/doc/samples/SqlCommand.cs b/doc/samples/SqlCommand.cs
new file mode 100644
index 0000000000..abce0fab23
--- /dev/null
+++ b/doc/samples/SqlCommand.cs
@@ -0,0 +1,41 @@
+//
+using System;
+using System.Data;
+using Microsoft.Data.SqlClient;
+
+
+namespace SqlCommandCS
+{
+ class Program
+ {
+ static void Main()
+ {
+ string str = "Data Source=(local);Initial Catalog=Northwind;"
+ + "Integrated Security=SSPI";
+ ReadOrderData(str);
+
+ }
+
+ private static void ReadOrderData(string connectionString)
+ {
+ string queryString =
+ "SELECT OrderID, CustomerID FROM dbo.Orders;";
+ using (SqlConnection connection = new SqlConnection(
+ connectionString))
+ {
+ SqlCommand command = new SqlCommand(
+ queryString, connection);
+ connection.Open();
+ using (SqlDataReader reader = command.ExecuteReader())
+ {
+ while (reader.Read())
+ {
+ Console.WriteLine(String.Format("{0}, {1}",
+ reader[0], reader[1]));
+ }
+ }
+ }
+ }
+ //
+ }
+}
diff --git a/doc/samples/SqlCommandBuilder.cs b/doc/samples/SqlCommandBuilder.cs
new file mode 100644
index 0000000000..bdde15518e
--- /dev/null
+++ b/doc/samples/SqlCommandBuilder.cs
@@ -0,0 +1,45 @@
+//
+using System;
+using System.Data;
+using Microsoft.Data.SqlClient;
+
+namespace SqlCommandBuilderCS
+{
+ class Program
+ {
+ static void Main()
+ {
+ string cnnst = "";
+ string queryst = "";
+ string tablen = "";
+ DataSet ds = SelectSqlRows(cnnst, queryst, tablen);
+
+ }
+
+ public static DataSet SelectSqlRows(string connectionString,
+ string queryString, string tableName)
+ {
+ using (SqlConnection connection = new SqlConnection(connectionString))
+ {
+ SqlDataAdapter adapter = new SqlDataAdapter();
+ adapter.SelectCommand = new SqlCommand(queryString, connection);
+ SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
+
+ connection.Open();
+
+ DataSet dataSet = new DataSet();
+ adapter.Fill(dataSet, tableName);
+
+ //code to modify data in DataSet here
+
+ builder.GetUpdateCommand();
+
+ //Without the SqlCommandBuilder this line would fail
+ adapter.Update(dataSet, tableName);
+
+ return dataSet;
+ }
+ }
+ //
+ }
+}
diff --git a/doc/samples/SqlCommand_ExecuteScalar.cs b/doc/samples/SqlCommand_ExecuteScalar.cs
new file mode 100644
index 0000000000..a6d81b71ef
--- /dev/null
+++ b/doc/samples/SqlCommand_ExecuteScalar.cs
@@ -0,0 +1,20 @@
+//
+using System;
+using System.Data;
+using Microsoft.Data.SqlClient;
+
+public class Sample
+{
+
+ public void CreateSqlCommand(
+ string queryString, SqlConnection connection)
+ {
+ SqlCommand command = new
+ SqlCommand(queryString, connection);
+ command.Connection.Open();
+ command.ExecuteScalar();
+ connection.Close();
+ }
+ //
+
+}
diff --git a/doc/samples/SqlCommand_Prepare.cs b/doc/samples/SqlCommand_Prepare.cs
new file mode 100644
index 0000000000..6812494a7f
--- /dev/null
+++ b/doc/samples/SqlCommand_Prepare.cs
@@ -0,0 +1,48 @@
+//
+using System;
+using System.Data;
+using Microsoft.Data.SqlClient;
+
+namespace SqlPrepareCS
+{
+ class Program
+ {
+ static void Main()
+ {
+ string connectionString = "Persist Security Info=False;Integrated Security=SSPI;database=Northwind;server=(local)";
+ SqlCommandPrepareEx(connectionString);
+ Console.ReadLine();
+
+ }
+ private static void SqlCommandPrepareEx(string connectionString)
+ {
+ using (SqlConnection connection = new SqlConnection(connectionString))
+ {
+ connection.Open();
+ SqlCommand command = new SqlCommand(null, connection);
+
+ // Create and prepare an SQL statement.
+ command.CommandText =
+ "INSERT INTO Region (RegionID, RegionDescription) " +
+ "VALUES (@id, @desc)";
+ SqlParameter idParam = new SqlParameter("@id", SqlDbType.Int, 0);
+ SqlParameter descParam =
+ new SqlParameter("@desc", SqlDbType.Text, 100);
+ idParam.Value = 20;
+ descParam.Value = "First Region";
+ command.Parameters.Add(idParam);
+ command.Parameters.Add(descParam);
+
+ // Call Prepare after setting the Commandtext and Parameters.
+ command.Prepare();
+ command.ExecuteNonQuery();
+
+ // Change parameter values and call ExecuteNonQuery.
+ command.Parameters[0].Value = 21;
+ command.Parameters[1].Value = "Second Region";
+ command.ExecuteNonQuery();
+ }
+ }
+ //
+ }
+}
diff --git a/doc/samples/SqlParameterCollection_AddWithValue.cs b/doc/samples/SqlParameterCollection_AddWithValue.cs
index 1864e3d98c..ee334d95de 100644
--- a/doc/samples/SqlParameterCollection_AddWithValue.cs
+++ b/doc/samples/SqlParameterCollection_AddWithValue.cs
@@ -1,8 +1,9 @@
+//
using System;
using System.Data;
-//
using Microsoft.Data.SqlClient;
+
class Program
{
static void Main()
@@ -52,4 +53,4 @@ static private string GetConnectionString()
+ "Integrated Security=SSPI";
}
}
-//
\ No newline at end of file
+//
diff --git a/doc/samples/SqlParameter_SqlParameter6.cs b/doc/samples/SqlParameter_SqlParameter6.cs
index 5034ad68d7..964723ba1d 100644
--- a/doc/samples/SqlParameter_SqlParameter6.cs
+++ b/doc/samples/SqlParameter_SqlParameter6.cs
@@ -10,7 +10,7 @@ static void Main()
}
private static void AddSqlParameter(SqlCommand command)
{
- SqlParameter parameter = new SqlParameter("@Description",
+ SqlParameter parameter = new SqlParameter("@Description",
SqlDbType.VarChar, 88, "Description");
parameter.IsNullable = true;
parameter.Direction = ParameterDirection.Output;
diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBulkCopyColumnMappingCollection.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBulkCopyColumnMappingCollection.cs
index 0627dd1918..5d72fc4dde 100644
--- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBulkCopyColumnMappingCollection.cs
+++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBulkCopyColumnMappingCollection.cs
@@ -72,6 +72,7 @@ public SqlBulkCopyColumnMapping Add(int sourceColumnIndex, int destinationColumn
return Add(new SqlBulkCopyColumnMapping(sourceColumnIndex, destinationColumnIndex));
}
+ ///
private void AssertWriteAccess()
{
if (ReadOnly)