我想对数据库进行还原和备份  弄了好多天了都完成不了 在网上找了很多 都是有用到 SQLDMO 的   但是关于SQLDMO的 都不懂    在VS里面 应该怎么调用它啊

解决方案 »

  1.   

    添加引用=》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");
    }
    }