如何使用.Net创建SqlServer数据库备份?
我想使用此 C# 代码进行数据库备份:
connect = new SqlConnection(con);
connect.Open();
// Execute SQL
SqlCommand command = new SqlCommand
(
@"backup database MY_database to disk='d:\SQLBackup\wcBackUp1.bak' with init, stats=10",
connect
);
command.ExecuteNonQuery();
connect.Close();
当我运行它时,显示以下错误消息:
无法打开备份设备 'd:\SQLBackup\wcBackUp1.bak'。 操作系统错误3(系统找不到指定的路径。)。
如果我将路径更改为 d:\wcBackUp1.bak 似乎没问题,没有错误,但该文件不存在,未生成。
如果我在 SQL 中运行该命令,我会收到一条消息,表明该命令已 100% 处理,但我没有看到该文件。
有人可以帮我吗?
I want to make a database backup with this C# code:
connect = new SqlConnection(con);
connect.Open();
// Execute SQL
SqlCommand command = new SqlCommand
(
@"backup database MY_database to disk='d:\SQLBackup\wcBackUp1.bak' with init, stats=10",
connect
);
command.ExecuteNonQuery();
connect.Close();
When I run it, the following error message shows up:
Cannot open backup device 'd:\SQLBackup\wcBackUp1.bak'.
Operating system error 3(The system cannot find the path specified.).
If I change the path to d:\wcBackUp1.bak
it seems to be ok, is without error, but the file does not exist, it was not generated.
If I run in SQL the command I have the message that it was 100% processed, but I didn`t see the file.
Could someone help me please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保位置“d:\SQLBackup\”存在于数据库服务器中而不是客户端计算机上。
Make sure the location "d:\SQLBackup\" exist in your database server and not on your client machine.
有两件事需要检查。
Sql 服务可能无权访问 d:\sqlbackup 文件夹。旧的 Sql 安装过去默认安装具有对计算机的完全访问权限的服务,但较新的实例加强了这一点。您可以尝试更改存储默认备份的目录的路径。
其次,如果 sql server 与您运行该程序的机器不在同一台机器上,那么您必须记住 D: 将是 sql server 上的 D: 而不是您本地机器上的 D:
Two things to check.
The Sql Service may not have access to the d:\sqlbackup folder. Old Sql installs used to default to install the service with full access to the machine, but newer instances tighten that up. You could try changing the path to the directory where the default backups are stored.
Secondly, if the sql server is not on the same machine that you are running this program, then you must remember that the D: will be the D: on the sql server and not your local machine
从根本上来说,运行 SQL Server 服务的 Windows 帐户必须对指定文件夹具有写入权限。
您可以通过查看 SQL Server 配置管理器中的 SQL Server 服务(查看“登录身份”列)来检查这是什么帐户。
使用 Explorer -> 检查该帐户对目标文件夹实际拥有的权限。右键文件夹->属性->安全->高级->有效的权限。
检查这是否是问题的一种方法是更改代码以备份到 SQL 实例的备份文件夹,其中权限可能是正确的。例如
Fundamentally, the Windows account that the SQL Server service runs under must have write permissions on the specified folder.
You can check what account this is by looking in SQL Server Configuration Manager, under SQL Server Services (look at the Log On As column)
Check what permissions that account actually has on the target folder using Explorer -> right click folder -> properties -> security -> advanced -> effective permissions.
One way to check that this is the problem is to change your code to back up to your SQL instance's backup folder, where the permissions are likely to be correct. For example