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..
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
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());
}
i get the following error
Restore failed for Server 'server = ABC;uid=sa;pwd=sasa'.