Click here to Skip to main content
16,020,714 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to backup database in c# coding through in one button click to crete the backup and one button click to restore to backup.
Posted

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
 
One way is that you use SQL for that. To backup, execute BACKUP DATATABSE[^] and to restore RESTORE[^]
 
Share this answer
 
Comments
Mehdi Gholam 2-Jan-12 4:22am    
5'ed
Wendelius 2-Jan-12 4:24am    
Thanks :)
 
Share this answer
 
Comments
Mehdi Gholam 2-Jan-12 4:22am    
5'ed
Abhinav S 2-Jan-12 4:26am    
Thanks Mehdi.
Prasad_Kulkarni 2-Jan-12 4:38am    
good one sir,My 5

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