如何使用.Net创建SqlServer数据库备份?

发布于 2024-10-22 17:21:15 字数 556 浏览 1 评论 0原文

我想使用此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦等七年七年为一梦 2024-10-29 17:21:15

确保位置“d:\SQLBackup\”存在于数据库服务器中而不是客户端计算机上。

Make sure the location "d:\SQLBackup\" exist in your database server and not on your client machine.

揪着可爱 2024-10-29 17:21:15

有两件事需要检查。

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

如梦亦如幻 2024-10-29 17:21:15

从根本上来说,运行 SQL Server 服务的 Windows 帐户必须对指定文件夹具有写入权限。

您可以通过查看 SQL Server 配置管理器中的 SQL Server 服务(查看“登录身份”列)来检查这是什么帐户。

使用 Explorer -> 检查该帐户对目标文件夹实际拥有的权限。右键文件夹->属性->安全->高级->有效的权限。

检查这是否是问题的一种方法是更改​​代码以备份到 SQL 实例的备份文件夹,其中权限可能是正确的。例如

C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup

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

C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文