942,513 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 2667
  • C# RSS
Nov 5th, 2009
0

backup and restore sql database

Expand Post »
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..
C# Syntax (Toggle Plain Text)
  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
C# Syntax (Toggle Plain Text)
  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'.
Similar Threads
Reputation Points: -1
Solved Threads: 23
Posting Whiz in Training
babbu is offline Offline
207 posts
since Jun 2009
Nov 5th, 2009
0
Re: backup and restore sql database
also tried the following code
C# Syntax (Toggle Plain Text)
  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.
Reputation Points: -1
Solved Threads: 23
Posting Whiz in Training
babbu is offline Offline
207 posts
since Jun 2009
Nov 5th, 2009
0
Re: backup and restore sql database
problm solved dont no how...thnx ne ways...
Reputation Points: -1
Solved Threads: 23
Posting Whiz in Training
babbu is offline Offline
207 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Add user control in application
Next Thread in C# Forum Timeline: Saving and Discarding mails





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC