Creating SQL Server backup file (.bak) with c# to any location

后端 未结 5 1733
感动是毒
感动是毒 2021-02-03 13:35

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 t

5条回答
  •  暖寄归人
    2021-02-03 13:58

    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's but 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

提交回复
热议问题