Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to restore my database using c# code ,So i googled and i found this link :

http://www.codeproject.com/Articles/123441/SQL-Server-Backup-and-Restore-Databases-using

The backUp part works great,but when i want to restore my bak file i got this error :

enter image description here

The restore code that i am using is :

Restore res = new Restore();

this.Cursor = Cursors.WaitCursor;
this.dataGridView1.DataSource = string.Empty;

try
{
    string fileName = this.txtFileName.Text;
    string databaseName = this.ddlDatabase.SelectedItem.ToString();

    res.Database = databaseName;
    res.Action = RestoreActionType.Database;
    res.Devices.AddDevice(fileName, DeviceType.File);

    this.progressBar1.Value = 0;
    this.progressBar1.Maximum = 100;
    this.progressBar1.Value = 10;

    res.PercentCompleteNotification = 10;
    res.ReplaceDatabase = true;
    res.PercentComplete += new PercentCompleteEventHandler(ProgressEventHandler);
    res.SqlRestore(srv);

    MessageBox.Show("Restore of " + databaseName + " Complete!", "Restore", 
        MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (SmoException exSMO)
{
    MessageBox.Show(exSMO.ToString());
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}
finally
{
    this.Cursor = Cursors.Default;
    this.progressBar1.Value = 0;
}

when i run this application separately it restores my bak file but when i put this part of code inside my application it doesn't work ,and i think it because of the connection between my application and db so how can i kill all connection using c# code before restoring ?

Best regards

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.