Skip to content

Error in restore sql in sql ...

ghasem deh

ghasem deh

Asked Sep 1, 2014, 10:03 AM EDT1 answer
Share
LinkedInX
hi guys ...
 i use this code for restore :
 
private void Restore(string filename)
Java
        {
            try
            {
                string command = @"ALTER DATABASE PSI SET SINGLE_USER with ROLLBACK IMMEDIATE " + " USE master " + " RESTORE DATABASE PSI FROM DISK= N'" + filename + "'";
                //string command = @"RESTORE DATABASE  DBName FROM DISK ='" + filename + "' ";
                this.Cursor = Cursors.WaitCursor;
                SqlCommand ocommand = null;
                SqlConnection oconnection = null;
                oconnection = new SqlConnection("Data Source=(local);Initial Catalog=DBName ;user id=sa");
                if (oconnection.State != ConnectionState.Open)
                oconnection.Open();
                ocommand = new SqlCommand(command, oconnection);
                ocommand.ExecuteNonQuery();
                this.Cursor = Cursors.Default;
                MessageBox.Show("??????? ??????? ??  ???? ??????? ??  ?? ?????? ????? ??");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : ", ex.Message);
            }
        }
 and button restore :
 
string filename = string.Empty;
Bash
           openFileDialog1.Filter = @"SQL Backup Files ALL Files (*.*) |*.*| (*.Bak)|*.Bak";
           openFileDialog1.FilterIndex = 1;
           openFileDialog1.Filter = @"SQL Backup Files (*.*)|";

         //  string DateDay = FarsiLibrary.Utils.PersianDate.Now.ToString().Subs  tring(0, 10);
         //    openFileDialog1.FileName = DateDay.Replace("/", "") + "_" +  DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString() +  "-" + DateTime.Now.Second.ToString();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                filename = openFileDialog1.FileName;
                Restore(filename);
            }
 
 
  but error : Pic

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • ghasem deh
    0
    this code is OK :

    C#
     private void Restore(string strFileName)
            {
               // try
               // {
                try
                {
                    if (strFileName != string.Empty)
                    {
                        string command =
                            "ALTER DATABASE PSI SET OFFLINE with ROLLBACK IMMEDIATE RESTORE DATABASE PSI FROM DISK = '" +
                            strFileName + "'" +
    C#
                            " WITH REPLACE";
                        var oConnection =
                            new SqlConnection("server=localhost;Integrated security=true;database=PSI");
                        if (oConnection.State != ConnectionState.Open)
                            oConnection.Open();
                        var oCommand = new SqlCommand(command, oConnection);
                        oCommand.ExecuteNonQuery();
                        MessageBox.Show(". ??????? ??????? ?? ???? ??????? ?? ?????? ????? ??");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
              //  catch (Exception ex)
              //  {
               //     MessageBox.Show("Error : ", ex.Message);
              //  }
    0