添加引用=》COM=》Microsoft SQLDMO Object Library private void BackUp() { try { this.Cursor = Cursors.WaitCursor; //create an instance of a server class SQLDMO._SQLServer srv = new SQLDMO.SQLServerClass(); //connect to the server srv.Connect(this.cboServers.Text,this.txtUser.Text,this.txtPassword.Text); //create a backup class instance SQLDMO.Backup bak = new SQLDMO.BackupClass(); //set the backup device = files property ( easy way ) bak.Devices = bak.Files; //set the files property to the File Name text box bak.Files = this.txtFile.Text; //set the database to the chosen database bak.Database = this.cboDatabase.Text; //perform the backup bak.SQLBackup(srv); MessageBox.Show("Database successfully backed up.", "Backup Successfull"); this.Cursor = Cursors.Default; } catch(Exception err) { this.Cursor = Cursors.Default; MessageBox.Show(err.Message,"Error"); } } private void Restore() { try { this.Cursor = Cursors.WaitCursor; //create an instance of a server class SQLDMO._SQLServer srv = new SQLDMO.SQLServerClass(); //connect to the server srv.Connect(this.cboServers.Text,this.txtUser.Text,this.txtPassword.Text); //create a restore class instance SQLDMO.Restore res = new SQLDMO.RestoreClass(); //set the backup device = files property ( easy way ) res.Devices = res.Files; //set the files property to the File Name text box res.Files = this.txtFile.Text; //set the database to the chosen database res.Database = this.cboDatabase.Text; // Restore the database res.ReplaceDatabase = true; res.SQLRestore(srv); MessageBox.Show("Database restored successfully.", "Restore Successfull"); this.Cursor = Cursors.Default; } catch(Exception err) { this.Cursor = Cursors.Default; MessageBox.Show(err.Message,"Error"); } }
{
try
{
this.Cursor = Cursors.WaitCursor;
//create an instance of a server class
SQLDMO._SQLServer srv = new SQLDMO.SQLServerClass();
//connect to the server
srv.Connect(this.cboServers.Text,this.txtUser.Text,this.txtPassword.Text);
//create a backup class instance
SQLDMO.Backup bak = new SQLDMO.BackupClass();
//set the backup device = files property ( easy way )
bak.Devices = bak.Files;
//set the files property to the File Name text box
bak.Files = this.txtFile.Text;
//set the database to the chosen database
bak.Database = this.cboDatabase.Text;
//perform the backup
bak.SQLBackup(srv);
MessageBox.Show("Database successfully backed up.", "Backup Successfull");
this.Cursor = Cursors.Default;
}
catch(Exception err)
{
this.Cursor = Cursors.Default;
MessageBox.Show(err.Message,"Error");
}
}
private void Restore()
{
try
{
this.Cursor = Cursors.WaitCursor;
//create an instance of a server class
SQLDMO._SQLServer srv = new SQLDMO.SQLServerClass();
//connect to the server
srv.Connect(this.cboServers.Text,this.txtUser.Text,this.txtPassword.Text);
//create a restore class instance
SQLDMO.Restore res = new SQLDMO.RestoreClass();
//set the backup device = files property ( easy way )
res.Devices = res.Files;
//set the files property to the File Name text box
res.Files = this.txtFile.Text;
//set the database to the chosen database
res.Database = this.cboDatabase.Text;
// Restore the database
res.ReplaceDatabase = true;
res.SQLRestore(srv);
MessageBox.Show("Database restored successfully.", "Restore Successfull");
this.Cursor = Cursors.Default;
}
catch(Exception err)
{
this.Cursor = Cursors.Default;
MessageBox.Show(err.Message,"Error");
}
}