Click here to Skip to main content
16,020,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void button1_Click(object sender, EventArgs e)
        {
            _BackupDatabase("master", "C:\backup");
            
        }
        public void _BackupDatabase(string databaseName, string filePath)
        {
            try
            {
                Server sqlServerInstance = new Server(
                new Microsoft.SqlServer.Management.Common.ServerConnection(
                new System.Data.SqlClient.SqlConnection(@"Data Source=GAURAV-31B52EEB\SQLEXPRESS;Initial Catalog=master;Integrated Security=True")));// your connection string I place mine for illustration.. DON'T HARDLY WRITE IT, pass it as argument or add it in application configuration 
                Backup backupMgr = new Backup();
                backupMgr.Devices.AddDevice(filePath, DeviceType.File);
                backupMgr.Database = databaseName;
                backupMgr.Action = BackupActionType.Database;
                backupMgr.SqlBackup(sqlServerInstance);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " " + ex.InnerException);
            }
        }


error
======================
{"Backup failed for Server 'GAURAV-31B52EEB\\SQLEXPRESS'. "}
Posted
Updated 2-Jan-12 0:01am
v2

Chances are it's a permissions problem.
Windows 7 restricts access to the root folder of any hard disk to Admin only: try writing your backup to a subfolder that has "any user" read and write permissions instead.
 
Share this answer
 
Hi use below sp
 

 
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[Sp_DatabaseBackup]
as
Begin
DECLARE @name VARCHAR(50) -- database name
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- file name for backup
DECLARE @fileDate VARCHAR(20) -- used for file name
SET @path = 'C:\Users\Publishing\Desktop\'
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb')
OPEN db_cursor 
FETCH NEXT FROM db_cursor INTO @name 
WHILE @@FETCH_STATUS = 0 
BEGIN 
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
BACKUP DATABASE @name TO DISK = @fileName
 FETCH NEXT FROM db_cursor INTO @name 
END 
CLOSE db_cursor 
DEALLOCATE db_cursor 
End
 
Share this answer
 
Hi,

try to use SQL Authentication to backup. modify your ConnectionString with one of the SQL server user.


hope that may help you,

thanks
-amit.
 
Share this answer
 
To get the real problem, examine the SQL Server error log for example using SSMS. You'll find the same error in errorlog, but you also find the cause for this error just after the message you posted. That message should explain why the backup failed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900