backup and restore sql database

Thread Solved

Join Date: Jun 2009
Posts: 147
Reputation: babbu has a little shameless behaviour in the past 
Solved Threads: 15
babbu babbu is offline Offline
Junior Poster

backup and restore sql database

 
0
  #1
Nov 5th, 2009
m using visual studio 2005 and sql server 2005.
i want to create a backup of the sql server which m doin using the following code..
  1. if (saveBackupDialog.ShowDialog() == DialogResult.OK)
  2. {
  3. SqlCommand cmd = new SqlCommand();
  4. cmd.CommandText = @"backup database dbname to disk =@loc with init,stats=10";
  5. cmd.Parameters.Add("@loc", SqlDbType.VarChar);
  6. cmd.Parameters["@loc"].Value = saveBackupDialog.FileName.ToString() + ".bak";
  7. cmd.Connection = Module1.sqlcon;
  8. Module1.sqlcon.Open();
  9. cmd.ExecuteNonQuery();
  10. Module1.sqlcon.Close();
  11. }

however m unable to restore the backup.
ive used a couple of codes
  1. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  2. {
  3. try
  4. {
  5.  
  6. Restore rDatabase = new Restore();
  7.  
  8. // Set the restore type to a database restore
  9. rDatabase.Action = RestoreActionType.Database;
  10.  
  11. // Assign a db to restore operation
  12. rDatabase.Database = "coloursoft";
  13.  
  14. // Set the backup device to restore from file
  15. BackupDeviceItem bkDevice = new BackupDeviceItem(openFileDialog1.FileName, DeviceType.File);
  16.  
  17. // Add the backup device to the restore type
  18. rDatabase.Devices.Add(bkDevice);
  19.  
  20. // Replace the Db if already exists
  21. rDatabase.ReplaceDatabase = true;
  22. Server srv = new Server("server = ABC;uid=sa;pwd=sasa");
  23.  
  24. if (File.Exists(openFileDialog1.FileName))
  25. // restore
  26. rDatabase.SqlRestore(srv);
  27. }
  28. catch (Exception ex)
  29. {
  30. MessageBox.Show(ex.ToString());
  31. }
i get the following error

Restore failed for Server 'server = ABC;uid=sa;pwd=sasa'.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 147
Reputation: babbu has a little shameless behaviour in the past 
Solved Threads: 15
babbu babbu is offline Offline
Junior Poster
 
0
  #2
Nov 5th, 2009
also tried the following code
  1. SqlConnection connect;
  2. string con = "Data Source = localhost; Initial Catalog=master ;Integrated Security = True;";
  3. connect = new SqlConnection(con);
  4. SqlCommand cmd = new SqlCommand();
  5. cmd.Connection = connect;
  6. cmd.CommandText = "use master";
  7. connect.Open();
  8. cmd.ExecuteNonQuery();
  9.  
  10. cmd.CommandText = @"restore database coloursoft from disk = @loc with replace, MOVE 'coloursoft' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\coloursoft.mdf',
  11. MOVE 'coloursoft_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\coloursoft_Log.ldf'
  12. ";
  13. cmd.Parameters.Add("@loc", SqlDbType.VarChar);
  14. cmd.Parameters["@loc"].Value = openFileDialog1.FileName.ToString();
  15.  
  16. Module1.sqlcon.Close();
  17. cmd.ExecuteNonQuery();
  18. connect.Close();
i get the following exception

Exclusive access could not be obtained because the database is in use.
RESTORE DATABASE is terminating abnormally.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 147
Reputation: babbu has a little shameless behaviour in the past 
Solved Threads: 15
babbu babbu is offline Offline
Junior Poster
 
0
  #3
Nov 5th, 2009
problm solved dont no how...thnx ne ways...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 525 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC