I have two connection strings, the first is to an existing database, and the second is to a different SQL Server. I need to copy the entire first database to the second one. This includes creating all the tables, constraints, and keys. I want to use the SqlConnection
object and do not have access to a hard drive.
EDIT 1:
I can create the db (code below) but I can't figure out how to programmatically set up the tables and then populate them. What is the best way to approach this?
public void Copy()
{
var connectionForExisting = "REDACTED";
var connectionForNew = "REDACTED";
var targetDbName = "REDACTED";
var source = new SqlConnection(connectionForExisting);
var target = new SqlConnection(connectionForNew);
try
{
source.Open();
}
catch (Exception e)
{
source.Close();
return;
}
try
{
target.Open();
}
catch (Exception e)
{
source.Close();
target.Close();
return;
}
var myCommand = new SqlCommand(String.Format(CreateNewDatabaseScript, targetDbName), target);
try
{
myCommand.ExecuteNonQuery();
}
catch (Exception e)
{
source.Close();
target.Close();
return;
}
}