title: How to backup and restore in c#.net
body: <p>I developed a windows C# .net application and I use Sqlserver database.
  I created database in a special user named <em>admin</em>.
  And now I want to backup and restore database.</p>
  
  <p>So I am successful in backing up but I get error while restoring</p>
  
  <pre><code> private void pictureBox6_Click(object sender, EventArgs e)
          {
              try
              {
                  SaveFileDialog f = new SaveFileDialog();
                  f.InitialDirectory = "D:\\";
                  f.Title = "HoghooghDastmozdBackup";
                  f.FileName = "HoghooghDastmozdBackup";
                  f.FilterIndex = 1;
                  f.OverwritePrompt = true;
                  f.Filter = @"SQL Backup files (*.BAK) |*.BAK|All files(*.*) |*.*";
                  if (f.ShowDialog() == DialogResult.OK)
                  {
                      SqlConnection sqlconn = new SqlConnection("Data Source=.;Initial Catalog=HoghooghDastmozd;Persist Security Info=True;User ID=admin;password=123456admin");
                      //   SqlCommand sqlcmd = new SqlCommand("BACKUP DATABASE HoghooghDastmozd TO  DISK =@n", sqlconn);
                      SqlCommand sqlcmd = new SqlCommand("BACKUP DATABASE [HoghooghDastmozd] TO  DISK = @n WITH NOFORMAT, NOINIT,  NAME = N'HoghooghDastmozd-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10",sqlconn);
                      sqlcmd.Parameters.AddWithValue("@n", f.FileName);
                      sqlconn.Open();
                      sqlcmd.ExecuteNonQuery();
                      sqlconn.Close();
                      MessageBox.Show("عملیات پشتیبانی موفق بود");
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
  
          }
  
  
   private void pictureBox5_Click(object sender, EventArgs e)
          {
              try
              {
                  OpenFileDialog f = new OpenFileDialog();
                  f.InitialDirectory = "D:\\";
                  f.Title = "HoghooghDastmozdBackup";
                  f.FileName = "HoghooghDastmozdBackup";
                  f.Filter = @"SQL Backup files (*.BAK) |*.BAK|All files(*.*) |*.*";
                  if (f.ShowDialog() == DialogResult.OK)
                  {
                      SqlConnection sqlconn = new SqlConnection("");
                      SqlCommand sqlcmd = new SqlCommand("ALTER DATABASE HoghooghDastmozd  SET SINGLE_USER with ROLLBACK IMMEDIATE " + "use master   " + "RESTORE DATABASE [HoghooghDastmozd] FROM  DISK = @n WITH REPLACE ", sqlconn);
                      sqlcmd.Parameters.AddWithValue("@n", f.FileName);
                      sqlconn.Open();
                      sqlcmd.ExecuteNonQuery();
                      sqlconn.Close();
                      MessageBox.Show("عملیات بازیابی موفق بود");
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
          }
  </code></pre>
  
  <p>Error Image: 
  <img src="https://i.stack.imgur.com/c9Fs9.png" alt="Error image"></p>
  
  <p>I have granted these permission but not work:</p>
  
  <pre><code>EXEC sp_addrolemember 'db_ddladmin', mobtaker;
  EXEC sp_addrolemember 'db_datareader', mobtaker;
  EXEC sp_addrolemember 'db_datawriter', mobtaker;
  EXEC sp_addrolemember 'db_owner', mobtaker;
  EXEC sp_addrolemember 'dbcreator', mobtaker;
  EXEC sp_addrolemember 'sysadmin', mobtaker;
  </code></pre>