Http://topic.csdn.net/u/20080709/10/9b299342-c509-4e8c-908f-982d186d4a99.html
Preface
Project because the database and masterProgramPut it on a computer, so for security purposes, we need to separate the database from the program, put the database in the Intranet, and do not publish it to the Internet. in this case, normal database operations are fine, that is, backing up and restoring the database requires remote backup and restoration.
What I implement is to use the xpcmd command of the database to temporarily open a LAN shared folder on the master program machine, and then enable xpcmd on the remote database to execute the net use command for connection sharing, then, the backup can be restored in the same way as the local machine.
A simple idea is like this. The disadvantage is that many databases will kill the DLL used by xpcmd, so it is not perfect.
Implementation
The specific implementation process is as follows:
1: open a shared folder on the local machine
Code
// Enable local sharing
PROCESS p = New Process ();
P. startinfo. filename = " Cmd.exe " ;
P. startinfo. useshellexecute = False ;
P. startinfo. redirectstandardinput = True ;
P. startinfo. redirectstandardoutput = True ;
P. startinfo. redirectstandarderror = True ;
P. startinfo. createnowindow = True ;
P. startinfo. Arguments = " /C net share Share Name (for example, test) = folder to be shared (for example, c: \ testfolder) " ;
P. Start ();
P. waitforexit ();
Note that the waitforexit method is very strange at the beginning, that is, the second backup after the program is started cannot be successful, it turns out that we did not wait until the net share command is fully executed to execute the net use shared folder, so it will certainly not succeed...
2: Start the xpcmd of the remote database. Code
// Enable xpcmd
String SQL = " Exec sp_configure 'show advanced options', 1 \ r \ n " +
" Reconfigure \ r \ n " +
" Exec sp_configure 'xp _ Your shell', 1; \ r \ n " +
" Reconfigure \ r \ n " ; // Execute this SQL
3: Execute the net use command to reference the shared folder enabled by the host where the main program is located. Code
String SQL = " Xp_mongoshell 'net use \\\\ " + DNS. gethostname () + " \ Shared name (for example, test) = User Password/User: domain \ Username '; " ; // Execute this SQL
Note that this DNS is used. gethostname (), we use a host name for connection. Originally, we used an intranet IP address, but the Intranet IP address of the machine is hard to obtain, and some do not have an intranet IP address, therefore, if you have a firewall, You need to configure the host name to allow access.
4: Remote sharing has been connected through xp_shareshell, the following Backup and restoration are simple. You only need to replace the original backup and restoration path with the current "\ host name \ shared directory name \ backup or restoration File. for example, the backup SQL is: Code
Stringbuilder SQL = New Stringbuilder ();
// Back up to local
SQL. append ( " Backup database [ " );
SQL. append (name of the database to be backed up );
SQL. append ( " ] To disk =' " );
SQL. append ( " \\\\ " + DNS. gethostname () + " \ Share Name " );
SQL. append ( " \\ " );
SQL. append (name of the backup file );
SQL. append ( " ' " );
Run this SQL statement. 5: do not forget to close the shared directory and xp_cmdshell. Code
// Disable sharing
PROCESS p = New Process ();
P. startinfo. filename = " Cmd.exe " ;
P. startinfo. useshellexecute = False ;
P. startinfo. redirectstandardinput = True ;
P. startinfo. redirectstandardoutput = True ;
P. startinfo. redirectstandarderror = True ;
P. startinfo. createnowindow = True ;
P. startinfo. Arguments = " /C net share Share Name/delete/y " ;
P. Start ();
P. waitforexit ();
Note "/delete/y" here, because the command "y" or "N" is prompted during execution of this DELETE command, so the prompt is displayed.
Code
// Disable xpcmd
String Sqlcmd = " Exec sp_configure 'show advanced options', 1; \ r \ n " +
" Reconfigure \ r \ n " +
" Exec sp_configure 'xp _ Your shell', 0; \ r \ n " +
" Reconfigure \ r \ n " ; // Execute this SQL
OK.