954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

backup and restore sql database

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'.

babbu
Posting Whiz in Training
208 posts since Jun 2009
Reputation Points: -1
Solved Threads: 23
 

also tried the following code

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();

i get the following exception
Exclusive access could not be obtained because the database is in use.
RESTORE DATABASE is terminating abnormally.

babbu
Posting Whiz in Training
208 posts since Jun 2009
Reputation Points: -1
Solved Threads: 23
 

problm solved dont no how...thnx ne ways...

babbu
Posting Whiz in Training
208 posts since Jun 2009
Reputation Points: -1
Solved Threads: 23
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You