private void CopyData(InstallSettings settings)
{
//Get source tables
SqlConnection connection = null;
var sourceTables = new Dictionary<string, string>();
try
{
connection = new SqlConnection(settings.GetPrimaryConnectionString());
connection.Open();
var adpater = new SqlDataAdapter("select T.name as tablename, S.name as schemaname from sys.tables T inner join sys.schemas S on T.schema_id = S.schema_id where T.name <> 'dtproperties' AND T.name <> 'sysdiagrams' order by tablename", connection);
DataSet ds = new DataSet();
adpater.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
sourceTables.Add((string)row["tablename"], (string)row["schemaname"]);
}
}
catch (Exception ex)
{
throw;
}
finally
{
if (connection != null)
connection.Close();
}
var tempFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempFolder);
//Create format file
foreach (string tableName in sourceTables.Keys)
{
//Get the format FROM THE SERVER!!!
string text = string.Empty;
text = settings.CloudDatabase + "." + sourceTables[tableName] + "." + tableName + " format nul -f \"" + Path.Combine(tempFolder, tableName + ".xml") + "\" -x -n -q -S " + settings.CloudServer + " -U " + settings.CloudUserName + " -P " + settings.CloudPassword;
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("bcp", text);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
if (result.Contains("Error ="))
throw new Exception("There was an error while creating the format file for table '" + tableName + "'.", new Exception(result));
}
//Create data file
foreach (string tableName in sourceTables.Keys)
{
var columnList = this.GetColumnList(settings.GetCloudConnectionString(), tableName);
string text = "\"select " + columnList + " from " + settings.PrimaryDatabase + "." + sourceTables[tableName] + "." + tableName + "\" queryout \"" + Path.Combine(tempFolder, tableName + ".dat") + "\" -n -q ";
if (settings.PrimaryUseIntegratedSecurity)
text += "-T";
else
text += "-S " + settings.PrimaryServer + " -U " + settings.PrimaryUserName + " -P " + settings.PrimaryPassword;
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("bcp", text);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
if (result.Contains("Error ="))
throw new Exception("There was an error while creating the data file for table '" + tableName + "'.", new Exception(result));
}
//Move data
foreach (string tableName in sourceTables.Keys)
{
string text = string.Empty;
text = settings.CloudDatabase + "." + sourceTables[tableName] + "." + tableName + " in \"" + Path.Combine(tempFolder, tableName + ".dat") + "\" -n -q -E -S " + settings.CloudServer + " -U " + settings.CloudUserName + " -P " + settings.CloudPassword; // +" -f " + Path.Combine(tempFolder, tableName + ".xml");
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("bcp", text);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
if (result.Contains("Error ="))
throw new Exception("There was an error while moving data for table '" + tableName + "'.", new Exception(result));
}
//Remove temp folder
Directory.Delete(tempFolder, true);
}