C# 备份远程SQL Server数据库
我想将远程SQL Server数据库备份到本地计算机上 到目前为止,我已经尝试:C# 备份远程SQL Server数据库,c#,sql-server,powershell,C#,Sql Server,Powershell,我想将远程SQL Server数据库备份到本地计算机上 到目前为止,我已经尝试: using (SqlConnection defaultSqlConnection = new SqlConnection(Constants.Database_Constants.GetConnectionStringFromProfile(Constants.Profiles.Staging))) { string pathDatabase = System.IO.Path.GetDirectoryNa
using (SqlConnection defaultSqlConnection = new SqlConnection(Constants.Database_Constants.GetConnectionStringFromProfile(Constants.Profiles.Staging)))
{
string pathDatabase = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\backup_production_database"+DateTime.UtcNow.ToString("d_MMM_yyyy_HH_mm_ss", CultureInfo.InvariantCulture)+".bak";
WriteLine("The backup will be stored in " + pathDatabase);
string backupDb = "BACKUP DATABASE DB_Staging TO DISK = '" + pathDatabase+ "' WITH INIT, COMPRESSION";
File.Create(pathDatabase);
using (SqlCommand backupCommand = new SqlCommand(backupDb, defaultSqlConnection))
{
defaultSqlConnection.Open();
backupCommand.ExecuteNonQuery();
}
}
但我得到了这个错误:
无法打开备份设备“XXXX\bin\Debug\backup\u production\u database22\u Mar\u 2017\u 08\u 04\u 42.bak”。
操作系统错误3(系统找不到指定的路径)。
备份数据库正在异常终止
我知道我可以使用SQLServerManagementStudio生成脚本,但这对我没有帮助。我希望在powershell脚本中自动备份数据库,而不必手动进入SQL Server Management Studio您无法创建从远程服务器到本地磁盘的直接备份
请查看此项。使用该方法,文件将写入SQL Server本地文件系统。。因此,这就是它不喜欢路径的原因。也许您应该从服务器复制pendrive上的db并粘贴到您的计算机上。如果要创建从远程SQL server到本地计算机文件系统的备份,您必须在本地计算机上创建指向本地文件系统上目录的共享,然后在服务器上的路径
\\YourPC\YourBackupShare\.
中使用UNC符号-前提是服务器可以连接到本地系统,并且运行远程SQL server实例的用户有权编写您自己的本地文件系统。。。。。