本文介绍了使用C#将SQL Server备份文件(.bak)创建到任何位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#编写简单的应用程序,这将允许我备份,压缩和通过ftp发送我的SQL Server数据库.我遇到的一个问题是,如果我尝试在"C:\ Program Files \ Microsoft SQL Server \ MSSQL.3 \ MSSQL \ Backup"之外的其他位置进行备份,则无法创建备份文件(.bak).或"C:\ Program Files \ Microsoft SQL Server \ MSSQL.3 \ MSSQL \ Data"文件夹.我了解这是一个优先问题.有人可以将我指向这些资源,或者在此处写一个简短的代码段,介绍如何以编程方式向我的系统上的任何文件夹添加此类权限.

问候克里斯(Kris)

解决方案

我假设您正在将计划程序作为计划任务运行...您是否为任务的执行用户授予了对目标文件夹的写入权限? /p>


拥有权限,您可以有2种情况:

  • Windows身份验证
  • 混合身份验证

如果您使用Windows身份验证,则会获取Windows用户的读写权限.否则为sql server服务帐户的权限.

这种行为对我来说很有意义,也许在您遇到的情况中很重要!

修改2:
我不想鼓励您这样做...当您弄乱了他们的ACL时,某些管理员可能会讨厌您但可以解决问题

btw:Magnus Johansson 已经给您一个尝试此"链接

无论采用哪种方法,请务必交出正确的用户(如上所述)!

(用于完整历史记录)
...

旁注:
我知道这不是您问题的确切答案,但我建议您smo生成备份...

喜欢

using Microsoft.SqlServer.Management.Smo;

var bdi = new BackupDeviceItem(/* your path inlcuding desired file */);
var backup = new Backup
{
    Database = /* name of the database */,
    Initialize = true
};

backup.Devices.Add(bdi);

var server = new Server(this.SqlServer);

try
{
    backup.SqlBackup(server);
}
catch (Exception ex)
{
    // * log or sth
}

您只需要关心.dll文件.为所需的服务器版本组装(某些参数/属性因服务器版本而异)
更多信息此处

I'm trying to write simple application in C# which will allow me to backup, zip and send over ftp my SQL Server database.One problem I have encountered is that I'm not able to create the backup file (.bak) if I try to do it in different location than "C:\Program Files\Microsoft SQL Server\MSSQL.3\MSSQL\Backup" or "C:\Program Files\Microsoft SQL Server\MSSQL.3\MSSQL\Data" folder. I understand that this is a premission problem. Could someone point me to the resources or write here a short snippet how to programmatically add such a permission to any folder on my system.

RegardsKris

解决方案

i assume you are running your programm as a scheduled task ... did you give writing permissions to the target folder for the executing user of the task??

edit:
with permissions you can have 2 scenarios:

  • windows authenification
  • mixed authentification

if you are using windows authentification, the read and write permissions of the windows user are taken. otherwise the permissions for the sql server service account.

and this behaviour makes sense to me and maybe hits the nail in your scenario!

edit 2:
i don't want to encourage you to do so ... some admins may hate you when you mess up their acl'sbut this may do the trick

btw: Magnus Johansson already gave you a "try-this" link

no matter for which method you go - be sure to hand in the correct user (as descriped above!)

(for full history)
...

side-note:
i know this is not the exact answer to your question, but i would recommend you smo to generate backups ...

like

using Microsoft.SqlServer.Management.Smo;

var bdi = new BackupDeviceItem(/* your path inlcuding desired file */);
var backup = new Backup
{
    Database = /* name of the database */,
    Initialize = true
};

backup.Devices.Add(bdi);

var server = new Server(this.SqlServer);

try
{
    backup.SqlBackup(server);
}
catch (Exception ex)
{
    // * log or sth
}

you only have to care for the .dll's. take assemblies for the desired server version (some params/properties vary through different server versions)
more info here

这篇关于使用C#将SQL Server备份文件(.bak)创建到任何位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 13:11