C # Two Methods for restoring sqlserver2000 Database Backup

Source: Internet
Author: User
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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.