| | |
backup and restore sql database
Thread Solved |
•
•
Join Date: Jun 2009
Posts: 147
Reputation:
Solved Threads: 15
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..
however m unable to restore the backup.
ive used a couple of codes
i get the following error
Restore failed for Server 'server = ABC;uid=sa;pwd=sasa'.
i want to create a backup of the sql server which m doin using the following code..
C# Syntax (Toggle Plain Text)
if (saveBackupDialog.ShowDialog() == DialogResult.OK) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = @"backup database dbname to disk =@loc with init,stats=10"; cmd.Parameters.Add("@loc", SqlDbType.VarChar); cmd.Parameters["@loc"].Value = saveBackupDialog.FileName.ToString() + ".bak"; cmd.Connection = Module1.sqlcon; Module1.sqlcon.Open(); cmd.ExecuteNonQuery(); Module1.sqlcon.Close(); }
however m unable to restore the backup.
ive used a couple of codes
C# Syntax (Toggle Plain Text)
if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { Restore rDatabase = new Restore(); // Set the restore type to a database restore rDatabase.Action = RestoreActionType.Database; // Assign a db to restore operation rDatabase.Database = "coloursoft"; // Set the backup device to restore from file BackupDeviceItem bkDevice = new BackupDeviceItem(openFileDialog1.FileName, DeviceType.File); // Add the backup device to the restore type rDatabase.Devices.Add(bkDevice); // Replace the Db if already exists rDatabase.ReplaceDatabase = true; Server srv = new Server("server = ABC;uid=sa;pwd=sasa"); if (File.Exists(openFileDialog1.FileName)) // restore rDatabase.SqlRestore(srv); } catch (Exception ex) { MessageBox.Show(ex.ToString()); }
Restore failed for Server 'server = ABC;uid=sa;pwd=sasa'.
•
•
Join Date: Jun 2009
Posts: 147
Reputation:
Solved Threads: 15
0
#2 Nov 5th, 2009
also tried the following code
i get the following exception
Exclusive access could not be obtained because the database is in use.
RESTORE DATABASE is terminating abnormally.
C# Syntax (Toggle Plain Text)
SqlConnection connect; string con = "Data Source = localhost; Initial Catalog=master ;Integrated Security = True;"; connect = new SqlConnection(con); SqlCommand cmd = new SqlCommand(); cmd.Connection = connect; cmd.CommandText = "use master"; connect.Open(); cmd.ExecuteNonQuery(); 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', MOVE 'coloursoft_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL.2\MSSQL\Data\coloursoft_Log.ldf' "; cmd.Parameters.Add("@loc", SqlDbType.VarChar); cmd.Parameters["@loc"].Value = openFileDialog1.FileName.ToString(); Module1.sqlcon.Close(); cmd.ExecuteNonQuery(); connect.Close();
Exclusive access could not be obtained because the database is in use.
RESTORE DATABASE is terminating abnormally.
![]() |
Similar Threads
- VB backup and restore program (Visual Basic 4 / 5 / 6)
- back-up & restore mysql database in runtime(help pls) (Visual Basic 4 / 5 / 6)
- Remote MS SQL database update issue (MS SQL)
- SQL Server 2005 - Merge Replication (MS SQL)
- SQL database backup (MS SQL)
- Back Up of SQL database in C# (C#)
- SQL Server 7.0 BackUP and Restore Code (Visual Basic 4 / 5 / 6)
- Use Backup to Restore Files and Folders on Your Computer in Windows XP (Windows tips 'n' tweaks)
Other Threads in the C# Forum
- Previous Thread: Add user control in application
- Next Thread: Saving and Discarding mails
Views: 525 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C#
.net 2d access ado.net algorithm application array asp.net automation bitmap box button c# calendar chat check checkbox class client code color combobox control conversion csharp custom data database datagrid datagridview dataset datetime degrees deployment display drawing excel file form format forms function gcd gdi+ graphics httpwebrequest image index input install java list listbox login mandelbrot math mysql networking notepad operator path pda picturebox pixelinversion prime print programming property regex remote remoting resource save saving search server socket sounds sql statistics string studio table tcp text textbox thread time timer treeview update usercontrol validation visual visualstudio webbrowser windows winforms wpf xml





