如何以编程方式恢复 SQL localDB

How to restore SQL localDB programmatically

我正在尝试为我的 Windows 表单应用程序创建备份和恢复功能。

所以,我尝试进行数据库还原。我的代码是这样的:

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

但是当我尝试 运行 时,出现错误:

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.

有人能帮帮我吗?

您是要备份还是还原?你在标题中提到备份,命令是恢复。

localDB 的例子还不多。

使用我的代码如下。希望这有帮助..

用于备份-

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

恢复-

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

更新-

糟糕,抱歉,我的代码适用于 SQL localDB 2014,但您似乎使用的是 2012。 请将 (LocalDB)\MSSQLLocalDB 替换为 (LocalDB)\v11.0

请尝试进行上述更改。

供您参考,根据我的经验,如果我写了 |Data Directory|在我的 connectionString 中,我只能读取(SQL SELECT 命令)数据库。换句话说,Insert和Update命令可以执行,但实际上没有插入或更新数据库,无一例外。我认为设置为|数据目录|将使数据库成为 read-only 我发现有些人在使用 |Data Directory| 插入或更新数据库时遇到困难设置。

希望你过得愉快!谢谢!