我最近使用以下代码在我的项目中创建了一个数据库恢复模块
string cbdfilename = "C:\\Users\\Public\\Bbcon.bak";
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\v11.0;Database=Master;Integrated Security=True;Connect Timeout=30;");
string sql = "Restore database @DBNAME from Disk = @FILENAME With Move 'BB'";
SqlConnection.ClearAllPools();
SqlCommand cmd = new SqlCommand(sql, con);
string database = Path.Combine(Environment.CurrentDirectory, @"BbCon.mdf");
cmd.Parameters.AddWithValue("@DBNAME", database);
cmd.Parameters.AddWithValue("@FILENAME", cbdfilename);
con.Open();
try
{
MessageBox.Show(cmd.ExecuteNonQuery().ToString());
}
catch (Exception ex)
{
MessageBox.Show("Backup DB failed" + ex.ToString());
}
finally
{
con.Close();
con.Dispose();
}
我使用visual studio调试时数据库i成功,所以我使用clickonce创建了一个安装文件,并在客户端系统上安装但显示错误