I am creating one windows desktop application but I am stucked at a point where I need guidance from SO developers.
My requirements :
My main requirement is to create a windows form application that can copy database of one system to another.
see below snap.

What I tried So Far?
I successfully create backup file and restored it in local system using C# but right now it only works for local system (System A).
What issues I am facing?
I want to achieve this both using C#. I can handle all this using shared folder but that's not recommended.
how to access backup generated in another system of LAN because we will never have access to other system's directory. I think I can do it with sharing folder, but is there any other way that I can copy backup file and store it in the system where my application is running.
How can I restore backup file to another LAN system. as I know SQL is not giving rights to restore databases from outside local directory.
Is there any other way to achieve this kind of (Copy database) functionality?
Any help related to flow or any reference will be appreciated.
You can create a backup of a DB into a remote location. For example, System B would be creating a backup in a shared folder of System C. Then you can restore the backup on System C. You could also do it the other way round, create a shared folder on System B, create the backup into this folder and restore the backup on System C from the remote location.
Considering the nature of what you want to accomplish, I assume the following:
D:\TEST\ is shared as \\SYSTEM C\TEST\.If all previous conditions can be met, then from the Application in System A you can execute the following command on System B:
BACKUP DATABASE [MyDataBase] TO DISK = N'\\SYSTEM C\TEST\MyDataBase.bak' WITH NOFORMAT, INIT, NAME = N'MyDataBase-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
Supposing \\SYSTEM C\TEST\ is the remote address for D:\TEST\ (in System C, obviously) then, from application in System A, you can execute the following command on System C:
RESTORE DATABASE [MyDataBaseCopy] FROM DISK = N'D:\TEST\MyDataBase.bak' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
Both commands can be exectuted from SqlCommand instances, one with a connection to System B and the other with a connection to System C.
Hope it helps.
EDIT 1
Thanks to @PMF's comments pointing out that there may be scenarios in which this solution, as is, won't work. In the case you get this kind of error:
Msg 3201, Level 16, State 1, Line 1
Cannot open backup device '\\SYSTEM C\TEST\MyDataBase.bak. Operating system error 5(Access is denied.).
You can try one of the following options given your scenario:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With