gpt4 book ai didi

c# - 如何以编程方式恢复 SQL localDB

转载 作者:行者123 更新时间:2023-12-03 22:38:10 25 4
gpt4 key购买 nike

我正在尝试为我的 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命令可以执行,但实际上没有插入或更新数据库,无一异常(exception)。我认为设置为|数据目录|将使数据库为只读,我发现有些人在使用 |Data Directory| 插入或更新数据库时遇到困难。设置。

希望你过得愉快!谢谢!

关于c# - 如何以编程方式恢复 SQL localDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42307537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com