0

I'm trying to make a backup to a network shared folder with the following code:

string filePath = ("\\pc-usuario\folder\backup\backup.bak")
string connectionString = String.Format(@"Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=True", server, database);

using (var connection = new SqlConnection(connectionString))
{
    var query = String.Format("BACKUP DATABASE [{0}] TO DISK='{1}'", database, filePath);

    using (var command = new SqlCommand(query, connection))
    {
        connection.Open();
        command.CommandTimeout = 1800;
        command.ExecuteNonQuery();
    }
}

Got the following error:

The backup device cannot be opened. Operating system error 5(Access denied.).

If I try it with SQLExpress the same error happens. What am I missing?

Lechucico
  • 1,914
  • 7
  • 27
  • 60
  • your code needs permission to wirte to the network share, therefore same rules as this thread applies: https://social.msdn.microsoft.com/Forums/en-US/a7b1045a-efa2-4cf3-bb26-b22c7b2c60d7/how-to-create-a-file-on-a-networkshared-folder?forum=csharpgeneral – Bacon Nov 11 '19 at 11:55
  • @Bacon I think SQL server need permission, not my code. – Lechucico Nov 11 '19 at 12:35
  • Possible duplicate of [Cannot open backup device. Operating System error 5](https://stackoverflow.com/questions/3960257/cannot-open-backup-device-operating-system-error-5) – SMor Nov 11 '19 at 12:44

1 Answers1

0

You havent given the database backup a file name, only a file path

E.g. the final code should read

backup database wibble to disk = '\\pc-usuario\folder\backup\wibble.bak'
uberbloke
  • 116
  • 6