Question

As per the below reference document, I am looking to move a SQL Server Express database (localdb created within vs2013).

How to move a localdb from one machine to another manually

However the operation needs to be entirely automated from within my wpf c# application.

What I want to achieve is (updated):

  1. Initial Application implementation (Currently most important) ->

    The database needs to be installed / copied to a users machine when the application is installed, ideally the database needs to be packaged in a zip or installer file, so it can be easily transferred to the users machine when installing the application, or I'm open to any better ideas?

  2. Keeping it upto date -> The application needs to be able to export and import data from the localdb to a sql database file on a local network share. This may require merging of data.

Whether it be done via c# code or sql script (if script please advise how to run a script from c#) How could one best achieve this?

Thanks for your help in advance!

Was it helpful?

Solution

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.

  1. Verify the primary database exist
  2. Issue a command to take a backup
  3. Create a new database
  4. Verify your newly created database exist
  5. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top