based on your question you're trying to simply transfer a database from one machine to another correct? You'll need to consider some variables, for instance do you need to create the database? How will the application speak to each machine? Several other variables will need to be considered; however the route I would more than likely take is create a desired web-service.
The web-service would allow you to decide if your pulling a local database to the server, or the server to your local machine, or any requirements that may be required.
Without those requirements the task becomes difficult. However, if you think of your issue on a smaller scale, it becomes quite clear.
- Verify the primary database exist
- Issue a command to take a backup
- Create a new database
- Verify your newly created database exist
- Run a command to restore a backup
That is essentially all you need to accomplish, a single class could do that.
You would need to create the logic for the methods this calls; but is an idea to do your request on the same machine; which isn't that different from your request.
if(IsDatabaseInExistence(server.TemplateName) == true)
CreateSQLDatabase(customer.WebAddress);
if(IsDatabaseInExistence(customer.WebAddress) == true)
RestoreSQLDatabase(server.TemplateName, customer.WebAddress);
A method that takes parameters, then call a couple private methods and you've accomplished your task. On your quest though to accomplish, try to use parameters
, if you issue raw SqlCommand
you can make it susceptible to SQL Injection.
Bad Practice:
using(SqlConnection connectionForSQL = new SqlConnection(@"Server=localhost; Integrated Security=SSPI; Database=master"))
{
string restoreSQLDb =
"RESTORE FILELISTONLY FROM DISK='C:\\Program Files\\Microsoft SQL Server\\MSSQL11.MSSQLSERVER\\MSSQL\\Backup\\" + templateName + ".bak'"
+ "RESTORE DATABASE [" + webAddress + "] FROM DISK='C:\\Program Files\\Microsoft SQL Server\\MSSQL11.MSSQLSERVER\\MSSQL\\Backup\\" + templateName + ".bak'"
+ "WITH "
+ "MOVE 'Parent' TO 'C:\\Program Files\\Microsoft SQL Server\\MSSQL11.MSSQLSERVER\\MSSQL\\DATA\\" + webAddress + ".mdf',"
+ "MOVE 'Parent_log' TO 'C:\\Program Files\\Microsoft SQL Server\\MSSQL11.MSSQLSERVER\\MSSQL\\DATA\\" + webAddress + "_log.ldf',"
+ "REPLACE";
using(SqlCommand restoreDbCommand = new SqlCommand(restoreSQLDb, connectionForSQL))
{
connectionForSQL.Open();
restoreDbCommand.ExecuteNonQuery();
}
}
Also, the example I forgot to issue a backup method but you get the idea.
However I think your issue is getting it from one machine to another. I would look into Windows Communication Foundation Services (WCF). This should provide the flexibility to communicate with another machine and accomplish an assortment of task.
Unfortunately, I don't have exact requirements so it makes it truly difficult to help you.
Hopefully this points you in the right direction though.