using System;
using System.Data;
using System.Data.SqlClient;

namespace BulkInsert
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
      Bulk1();
		}

		static void Bulk1()
		{
			string connect_string = GetConnectStringFromConfigFile();
			SqlConnection conn = new SqlConnection(connect_string);
			SqlCommand createcmd = new SqlCommand (
				@"create table newjobs(
					job_id smallint primary key,
					job_desc varchar(30),
					min_lvl tinyint,
					max_lvl tinyint)",
				conn);
			SqlCommand cmd = new SqlCommand(
			"select * from jobs", conn);
			conn.Open();
			SqlDataReader rdr = cmd.ExecuteReader();
			// Copy the Data to SqlServer
			connect_string = GetConnectStringFromConfigFile();
			SqlBulkCopy bcp = new SqlBulkCopy(connect_string);
			bcp.DestinationTableName = "newjobs";
			bcp.WriteToServer(rdr);
		}

		static void Bulk2()
		{
			// Retrieve data from the source server.
			string src_connect_string = GetConnectStringFromConfigFile("src");
			SqlConnection src = new SqlConnection(src_connect_string);
			src.Open();
			SqlCommand cmd = new SqlCommand("select * from orders", src);
			IDataReader srcrdr = cmd.ExecuteReader();
			// Connect to target server.
			string dest_connect_string = GetConnectStringFromConfigFile("dest");
			SqlConnection dest = new SqlConnection(dest_connect_string);
			dest.Open();
			// open a bulk copy using the destination connection
			SqlBulkCopy bcp = new SqlBulkCopy(dest);
			{
				bcp.DestinationTableName = "order_history";
				// map the columns
				bcp.ColumnMappings.Add("orderid", "order_hist_id");
				bcp.ColumnMappings.Add("description", "order_hist_desc");
				bcp.ColumnMappings.Add("date", "order_hist_date");
				bcp.WriteToServer(srcrdr);
			}
			dest.Dispose();
			srcrdr.Close();
			cmd.Dispose();
			src.Dispose();
		}

		static string GetConnectStringFromConfigFile()
		{
			return "server=.;integrated security=sspi;database=pubs";
		}

		static string GetConnectStringFromConfigFile(string direction)
		{
			return "server=.;integrated security=sspi;database=pubs";
		}

	}
}
