How to restore SQL localDB programmatically

2

I'm trying to create backup and restore functionality to my Windows form application.

So, I've tried to do a database restore. My code is like this:

string cbdfilename = "c:\\Users\\Public\\Public Document";
SqlConnection.ClearAllPools();
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\BbCon.mdf;Integrated Security=True;Connect Timeout=30;");
string sql;

sql = "Use master;Alter Database BbCon Set Single_User With Rollback Immediate;Restore Database BbCon From Disk = @FILENAME With Replace;Alter Database BbCon Set Multi_User;";

SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@FILENAME", cbdfilename);

con.Open();

try
{
    cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
    MessageBox.Show("Restore DB failed" + ex.ToString());
}
finally
{
    con.Close();
    con.Dispose();
}

But when I try to run this, I get an error:

Restore db failed System.Data.SqlClient.SqlException(0X80131904):userdoesnot have permission to alter database BbCon.mdf the database doesnot exist or or datbase is not in a state that allows access checks.

Can anyone help me please?

c#
sql-server
restore
localdb
asked on Stack Overflow Feb 17, 2017 by shahid khan • edited Feb 20, 2017 by Kay Lee

1 Answer

5

Are you trying to Backup or Restore? You mention Backup in the Title and the commmand is for Restore.

There're not many example cases for localDB yet.

Utilize my code as below. Hope this helps..

For Backup-

string master_ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;Database=Master;Integrated Security=True;Connect Timeout=30;";

using (SqlConnection masterdbConn = new SqlConnection())
{
     masterdbConn.ConnectionString = master_ConnectionString;
     masterdbConn.Open();

     using (SqlCommand multiuser_rollback_dbcomm = new SqlCommand())
     {
         multiuser_rollback_dbcomm.Connection = masterdbConn;
         multiuser_rollback_dbcomm.CommandText= @"ALTER DATABASE yourdbname SET MULTI_USER WITH ROLLBACK IMMEDIATE";

         multiuser_rollback_dbcomm.ExecuteNonQuery();
     }
     masterdbConn.Close();
}

SqlConnection.ClearAllPools();

using (SqlConnection backupConn = new SqlConnection())
{
    backupConn.ConnectionString = yourConnectionString;
    backupConn.Open();

    using (SqlCommand backupcomm = new SqlCommand())
    {
        backupcomm.Connection = backupConn;
        backupcomm.CommandText= @"BACKUP DATABASE yourdbname TO DISK='c:\yourdbname.bak'";
        backupcomm.ExecuteNonQuery();
    }
    backupConn.Close();
}

For Restore-

string master_ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;Database=Master;Integrated Security=True;Connect Timeout=30;";

using (SqlConnection restoreConn = new SqlConnection())
{
    restoreConn.ConnectionString = master_ConnectionString;
    restoreConn.Open();

    using (SqlCommand restoredb_executioncomm = new SqlCommand())
    {
        restoredb_executioncomm.Connection = restoreConn;
        restoredb_executioncomm.CommandText = @"RESTORE DATABASE yourdbname FROM DISK='c:\yourdbname.bak'";

        restoredb_executioncomm.ExecuteNonQuery();
    }
    restoreConn.Close();
}

Update-

Oops, sorry, my code is for SQL localDB 2014 but it seems you're using 2012. Kindly replace (LocalDB)\MSSQLLocalDB to (LocalDB)\v11.0

And kindly just try with above change.

And for your information, as per my experience, if I wrote |Data Directory| in my connectionString, I could only read (SQL SELECT command) the Database. In other word to say, the Insert and Update commands could be done but the database was not inserted or updated actually without any exception. I think setting as |Data Directory| would make the database as read-only and I've found some people were having difficulties to insert or update to database with |Data Directory| setting.

Hope your good days ! Thanks !

answered on Stack Overflow Feb 18, 2017 by Kay Lee • edited Feb 19, 2017 by Kay Lee

User contributions licensed under CC BY-SA 3.0