Back up and restore databases using. net
C # Two Methods for restoring sqlserver2000 Database Backup
: Method 1 (sqldmo is not used ):
///
/// Backup method
///
Sqlconnection conn = new sqlconnection ("Server =.; database = Master; user id = sa; Password = sa ;");
Sqlcommand cmdbk = new sqlcommand ();
Cmdbk. commandtype = commandtype. text;
Cmdbk. Connection = conn;
Cmdbk. commandtext = @ "backup database test to disk = 'C:/Ba' with init ";
Try
{
Conn. open ();
Cmdbk. executenonquery ();
MessageBox. Show ("backup successed .");
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
Conn. Close ();
Conn. Dispose ();
}
///
/// Restore Method
///
Sqlconnection conn = new sqlconnection ("Server =.; database = Master; user id = sa; Password = sa; trusted_connection = false ");
Conn. open ();
// Kill database Process
Sqlcommand cmd = new sqlcommand ("select spid from sysprocesses, sysdatabases where sysprocesses. dbid = sysdatabases. dbid and sysdatabases. Name = 'test'", Conn );
Sqldatareader Dr;
Dr = cmd. executereader ();
Arraylist list = new arraylist ();
While (dr. Read ())
{
List. Add (dr. getint16 (0 ));
}
Dr. Close ();
For (INT I = 0; I <list. Count; I ++)
{
Cmd = new sqlcommand (string. Format ("kill {0}", list), Conn );
Cmd. executenonquery ();
}
Sqlcommand cmdrt = new sqlcommand ();
Cmdrt. commandtype = commandtype. text;
Cmdrt. Connection = conn;
Cmdrt. commandtext = @ "Restore database test from disk = 'C:/Ba '";
Try
{
Cmdrt. executenonquery ();
MessageBox. Show ("Restore successed .");
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
Conn. Close ();
}
Method 2 (using sqldmo ):
///
/// Backup method
///
Sqldmo. Backup = new sqldmo. backupclass ();
Sqldmo. sqlserver Server = new sqldmo. sqlserverclass ();
// Display the progress bar
Sqldmo. backupsink_percentcompleteeventhandler progress = new sqldmo. backupsink_percentcompleteeventhandler (STEP );
Backup. percentcomplete + = progress;
Try
{
Server. loginsecure = false;
Server. Connect (".", "sa", "sa ");
Backup. Action = sqldmo. sqldmo_backup_type.sqldmobackup_database;
Backup. Database = "test ";
Backup. Files = @ "D:/test/myprog/backuptest ";
Backup. backupsetname = "test ";
Backup. backupsetdescription = "backup the database of test ";
Backup. initialize = true;
Backup. sqlbackup (server );
MessageBox. Show ("backup successed .");
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
Server. Disconnect ();
}
This. pbdb. value = 0;
///
/// Restore Method
///
Sqldmo. Restore restore = new sqldmo. restoreclass ();
Sqldmo. sqlserver Server = new sqldmo. sqlserverclass ();
// Display the progress bar
Sqldmo. restoresink_percentcompleteeventhandler progress = new sqldmo. restoresink_percentcompleteeventhandler (STEP );
Restore. percentcomplete + = progress;
// Kill database Process
Sqlconnection conn = new sqlconnection ("Server =.; database = Master; user id = sa; Password = sa; trusted_connection = false ");
Conn. open ();
Sqlcommand cmd = new sqlcommand ("select spid from sysprocesses, sysdatabases where sysprocesses. dbid = sysdatabases. dbid and sysdatabases. Name = 'test'", Conn );
Sqldatareader Dr;
Dr = cmd. executereader ();
Arraylist list = new arraylist ();
While (dr. Read ())
{
List. Add (dr. getint16 (0 ));
}
Dr. Close ();
For (INT I = 0; I <list. Count; I ++)
{
Cmd = new sqlcommand (string. Format ("kill {0}", list), Conn );
Cmd. executenonquery ();
}
Conn. Close ();
Try
{
Server. loginsecure = false;
Server. Connect (".", "sa", "sa ");
Restore. Action = sqldmo. sqldmo_restore_type.sqldmorestore_database;
Restore. Database = "test ";
Restore. Files = @ "D:/test/myprog/backuptest ";
Restore. filenumber = 1;
Restore. replacedatabase = true;
Restore. sqlrestore (server );
MessageBox. Show ("Restore successed .");
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
Finally
{
Server. Disconnect ();
}
This. pbdb. value = 0;