I am using this code for backup database from .mdf file.
Backup databaseBackup = new Backup();
databaseBackup.Action = BackupActionType.Database;
databaseBackup.Database = CvVariables.Catalog;
databaseBackup.Devices.Add(new BackupDeviceItem(new NecessaryFunction().MsSqlBackupFileName(this.backupTextboxPath.Text), DeviceType.File));
Server databaseServer = new Server(@".\SQLEXPRESS");
MessageBox.Show(databaseServer.ToString());
databaseBackup.SqlBackup(databaseServer);
On my developer PC this code works fine. But on my client`s PC it throw this exception:
Backup Failed for Server ‘xxxxx/SQLEXPRESS’
Microsoft.SqlServer.Management.Common.ExecutionFailureException: An exception occurred while executing a Transact-SQL statement or batch. —> System.Data.SqlClient.SqlException:
Database ‘Cafeteria_Vernier_db’ does not exist. Make sure that the name is
entered correctly.
BACKUP DATABASE is terminating abnormally.
at Microsoft.SqlServer.Management.Common.ConnectionManager.ExecuteTSql(ExecuteTSqlAction
action, Object execObject, DataSet fillDataSet, Boolean
catchException)
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String
sqlCommand, ExecutionTypes executionType)
— End of inner exception stack trace —
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String
sqlCommand, ExecutionTypes executionType)
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(StringCollection
sqlCommands, ExecutionTypes executionType)
at Microsoft.SqlServer.Management.Smo.ExecutionManager.ExecuteNonQuery(StringCollection
queries)
at Microsoft.SqlServer.Management.Smo.BackupRestoreBase.ExecuteSql(Server
server, StringCollection queries)
at Microsoft.SqlServer.Management.Smo.Backup.SqlBackup(Server srv)
What am I doing wrong?
It looks like the security context that you are executing on the client PC is different than that on your developer PC. Verify that your client PC credentials have access to the Cafeteria database, otherwise it would get you that same message that it couldn’t be found (because it doesn’t have access to it).
My guess is that the client SQL Server login doesn’t have a mapped database user in the
Cafeteria_Vernier_dbdatabase.