1

I've got a backup of my database.

After editing it in my program, it's a new newDb.bak file(on my PC).

How could I restore this new database on the remote SQL Server, if I have the connection string (I know ip, login and password)?

Mainly I have to do this only using C# (not the SQL Server Management Studio).

1 Answer 1

3

Unfortunately there is no any direct way to restore database remotely from local backup file.

But the alternative solution is making a shared location in some where (local side or server side it's not matter) which both of the client and server have access to there. (Update: Also, make sure the SQL server that will be performing the restore has permissions to the directory that you're copying the backup to. – @KSib) Then restore the database from file :

  1. Copy newDb.bak to \\serverip\$sharedfolder\newDb.bak on the server.
  2. RESTORE DATABASE YOURDB FROM DISK = N'\\serverip\$sharedfolder\newDb.bak';

if you want i can share the source code of restoring database in C#.

Update: Another solution is that create a table which contains varbinary column, Insert the backup file content from local to it by SqlCommand or SqlBulkCopy to and then export the file content to some where in your server by bcp such this:

BCP "SELECT FileContent FROM createdTable WHERE ID = 1000" queryout "C:\newDB.bak" -T

Then you can execute the restore command easily!

Also you can create a server side StoredProcedure containing a transaction to get the file content as input parameter and do all the command as one transaction.

I hope to be helpful for you :).

Sign up to request clarification or add additional context in comments.

4 Comments

> there is no any direct way Oooh crap.
Also, make sure the SQL server that will be performing the restore has permissions to the directory that you're copying the backup to.
@KamikyIT I found a new way and update my post. Let me know if is not clear :)
@KSib I updated my answer and I put your recommendation in it. Regards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.