这个问题在Stackoverflow中得到了很多次回答,但我没有为我的项目找到合适的解决方案。
让我先给你看我的代码:
namespace ConsoleDBManagement
{
class Program
{
static void Main(string[] args)
{
//Metioned here your database name
string dbname = "newDb";
SqlConnection sqlcon = new SqlConnection();
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
sqlcon.ConnectionString = @"Server=ABC-PCSQLEXPRESS;database=" + dbname + ";uid=dran;pwd=sri;";
//Enter destination directory where backup file stored
string destdir = "D:\Working Projects";
//Check that directory already there otherwise create
if (!System.IO.Directory.Exists(destdir))
{
System.IO.Directory.CreateDirectory("D:\Working Projects");
}
try
{
//Open connection
sqlcon.Open();
//query to take backup database
//System.IO.File.Create("D:\Working Projects\FullBackUp.BAK");
sqlcmd = new SqlCommand("backup database newDb to disk='" + destdir + "\FullBackUp.BAK'", sqlcon);
sqlcmd.ExecuteNonQuery();
//Close connection
sqlcon.Close();
//Response.Write("Backup database successfully");
}
catch (Exception ex)
{
//Response.Write("Error During backup database!");
}
}
}
}
执行查询时出现异常。
无法打开备份设备"D:\Working Projects\FullBackUp.BAK"。操作系统错误3(系统找不到指定的路径。)。
BACKUP DATABASE正在异常终止。
请给我你的建议。
当您在使用SQL身份验证登录时运行备份和/或其他与外部文件相关的命令时,Windows安全上下文是SQL服务的安全上下文。
您的问题与备份权限重复。授予SQL服务帐户/组的权限,或者使用对路径具有权限的Windows身份验证登录名运行备份。