none
problem with smo

    Question

  • hi all

    i want restore my backup with this code but then i use it then dont affect anything.

    i dont get any error and it do nothing.

    public void RestoreDatabase()
            {
                string sConnect = Properties.Settings.Default.VideoArchiveDBConnectionString;
                string dbName;
                
                using (SqlConnection cnn = new SqlConnection(sConnect))
                {
                    try
                    {
                        cnn.Open();
                        dbName = cnn.Database.ToString();
    
                        ServerConnection sc = new ServerConnection(cnn);
                        Server sv = new Server(sc);
                        // Check that I'm connected to the user instance
    
                        OpenFileDialog openFile = new OpenFileDialog();
                        //openFile.ShowDialog();
                        // Create backup device item for the backup
                        BackupDeviceItem bdi = new BackupDeviceItem(@"C:\Backup.bak", DeviceType.File);
    
                    
                        /*SqlCommand com = new SqlCommand();
                        string UseMaster = "USE master";
                        string restore = "RESTORE DATABASE Battalion FROM DISK ='c:\\Backup.bak' WITH FILE = 1 , REPLACE";
                        string Alter1 = @"ALTER DATABASE [" + dbName + "] SET Single_User WITH Rollback Immediate";
                        string Alter2 = @"ALTER DATABASE [" + dbName + "] SET Multi_User";
                        // sv.Databases[dbName].Drop();
                        cnn.ChangeDatabase("Master");
                        com.Connection = cnn;
                        com.CommandText = UseMaster;
                        com.ExecuteNonQuery();
                        com.CommandText = Alter1;
                        com.ExecuteNonQuery();
                        com.CommandText = restore;
                        com.ExecuteNonQuery();*/
                        // Create the backup informaton
                        Backup bk = new Backup();
                        Restore res = new Restore();
    
                        res.Devices.AddDevice(@"C:\Backup.bak", DeviceType.File);
                        res.Database = "VideoArchiveDB";
                        res.Action = RestoreActionType.Database;
                        res.ReplaceDatabase = true;
                        res.NoRecovery = false;
                        res.ClearSuspectPageTableAfterRestore = true;
                        res.SqlRestore(sv); 
                        SqlConnection.ClearAllPools();
                    }
                    catch (Exception ex)
                    {
                        
                    }
                } 
    

    can anyone help me?

    thankful.

    Wednesday, January 11, 2012 4:25 AM

All replies

  • Rastinrastini,

    Is the backup valid ?

    Try to restore your backup using the TSQL command, refer  http://msdn.microsoft.com/en-us/library/ms186858(v=SQL.90).aspx for more information.


    Thanks
    Manish

    Please use Mark as Answer if my post solved your problem and use Vote As Helpful if a post was useful.
    Wednesday, January 11, 2012 4:43 AM
  • thanks for answer.

    i used this code :(am i correctly use this?)

    SqlCommand com = new SqlCommand();
                        string UseMaster = "USE master";
                        string restore = "RESTORE DATABASE Battalion FROM DISK ='c:\\Backup.bak' WITH FILE = 1 , REPLACE";
                        string Alter1 = @"ALTER DATABASE [" + dbName + "] SET Single_User WITH Rollback Immediate";
                        string Alter2 = @"ALTER DATABASE [" + dbName + "] SET Multi_User";
                        // sv.Databases[dbName].Drop();
                        cnn.ChangeDatabase("Master");
                        com.Connection = cnn;
                        com.CommandText = UseMaster;
                        com.ExecuteNonQuery();
                        com.CommandText = Alter1;
                        com.ExecuteNonQuery();
                        com.CommandText = restore;
                        com.ExecuteNonQuery();

    but dont restore my backup.

    using this code for backup but dont restore :

    SqlCommand com = new SqlCommand();
                    string UseMaster = "USE master";
                    string restore = "BACKUP DATABASE VideoArchiveDB TO DISK = 'C:\\Backup.bak' ";
                    string Alter1 = @"ALTER DATABASE [" + dbName + "] SET Single_User WITH Rollback Immediate";
                    string Alter2 = @"ALTER DATABASE [" + dbName + "] SET Multi_User";
                    // sv.Databases[dbName].Drop();
                    cnn.ChangeDatabase("Master");
                    com.Connection = cnn;
                    com.CommandText = UseMaster;
                    com.ExecuteNonQuery();
                    com.CommandText = Alter1;
                    com.ExecuteNonQuery();
                    com.CommandText = restore;
                    com.ExecuteNonQuery();


    how can correct this?

    thankful.

     



    Wednesday, January 11, 2012 4:57 AM
  • 
    Rastinrastin,
    

    I suggest you to first execute this query on your SQL Server,
    RESTORE DATABASE Battalion FROM DISK ='c:\Backup.bak' WITH FILE = 1 , REPLACE
    
    and let us know about if any error appears to you.

    Thanks
    Manish

    Please use Mark as Answer if my post solved your problem and use Vote As Helpful if a post was useful.
    Wednesday, January 11, 2012 5:15 AM
  • my db is Server-Base DataBase from visual C# "addnew item"

    can run this query on it?

    Wednesday, January 11, 2012 5:24 AM
  • Rastinrastini,

    I am not sure about it,

    I suggest you to refer http://www.codeproject.com/KB/database/BackupRestoreWithSmo.aspx

    http://www.sqldbatips.com/showarticle.asp?ID=40

    http://stackoverflow.com/questions/3094609/use-all-backup-sets-to-restore-database-with-smo


    Thanks
    Manish

    Please use Mark as Answer if my post solved your problem and use Vote As Helpful if a post was useful.
    Wednesday, January 11, 2012 5:29 AM
  • attached db and use this but it dont restore and get no error

     

     


    Wednesday, January 11, 2012 5:34 AM
  • can anyone help me?

    thankful.

    Wednesday, January 11, 2012 6:06 AM
  • Rastinrastini,

    Since you are not able to execute the restore query on the SSMS,

    I suggest to post this question in Visual C# http://social.msdn.microsoft.com/Forums/en-US/category/visualcsharp forum.


    Thanks
    Manish

    Please use Mark as Answer if my post solved your problem and use Vote As Helpful if a post was useful.
    Wednesday, January 11, 2012 6:50 AM
  • ok.

    very thankful for your help.

    Wednesday, January 11, 2012 8:44 AM
  • may my problrm cause by this?

    Error    3    Unable to copy file "C:\Users\mohammad\documents\visual studio 2010\Projects\Tests\FinalVideoArchive1\FinalVideoArchive1\DB\VideoArchiveDB.mdf" to "bin\Debug\DB\VideoArchiveDB.mdf". The process cannot access the file 'bin\Debug\DB\VideoArchiveDB.mdf' because it is being used by another process.    FinalVideoArchive1
    thankful.

    Wednesday, January 11, 2012 10:12 AM
  • anyone can help me?
    Wednesday, January 11, 2012 2:06 PM