Restore & Backup database using Code
How to restore & backup sql database using C# code?Plz tell me........
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted May 15, 2012, 9:08 AM EDT
Please refer the below links
http://midnightprogrammer.net/post/BackupRestore-SQL-database-using-C.aspx
http://www.codeproject.com/Tips/279705/Backup-Restore-of-SQL-Server-database-using-VB-NET
Thanks
SenthilkumarRank #387 · 6.5K reputationPosted May 15, 2012, 9:59 PM EDT
I have written an article and published here in C# corner.
http://www.c-sharpcorner.com/Blogs/8679/backup-and-restore-the-database-in-Asp-Net-web-application.aspx
The source code also attached, you can download it and can use the same.
Kunal VaishyaRank #990 · 2K reputationPosted May 15, 2012, 9:19 AM EDT
Imports System.Data.SqlClient
Public Class Form1
Dim con As SqlConnection
Dim cmd As SqlCommand
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=DBNAME")
End Sub
' Backup
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
cmd = New SqlCommand("backup database DBNAME to disk='\\server\path\filename.bak'", con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
' Restore
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
cmd = New SqlCommand("restore database DBNAME from disk='\\server\path\filename.bak'", con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Sub
End Class
Please check this thread for detail:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2568063&SiteID=1
Kunal VaishyaRank #990 · 2K reputationPosted May 15, 2012, 9:23 AM EDT
try this
DECLARE @DBName varchar(255) DECLARE @DATABASES_Fetch int DECLARE DATABASES_CURSOR CURSOR FOR select DATABASE_NAME = db_name(s_mf.database_id) from sys.master_files s_mf where -- ONLINE s_mf.state = 0 -- Only look at databases to which we have access and has_dbaccess(db_name(s_mf.database_id)) = 1 -- Not master, tempdb or model and db_name(s_mf.database_id) not in ('Master','tempdb','model') group by s_mf.database_id order by 1 OPEN DATABASES_CURSOR FETCH NEXT FROM DATABASES_CURSOR INTO @DBName WHILE @@FETCH_STATUS = 0 BEGIN declare @DBFileName varchar(256) set @DBFileName = datename(dw, getdate()) + ' - ' + replace(replace(@DBName,':','_'),'\','_') exec ('BACKUP DATABASE [' + @DBName + '] TO DISK = N''c:\db backup\' + @DBFileName + ''' WITH NOFORMAT, INIT, NAME = N''' + @DBName + '-Full Database Backup'', SKIP, NOREWIND, NOUNLOAD, STATS = 100') FETCH NEXT FROM DATABASES_CURSOR INTO @DBName END CLOSE DATABASES_CURSOR DEALLOCATE DATABASES_CURSORPravinkumar BirajdarRank #4,618 · 334 reputationPosted May 16, 2012, 6:23 AM EDT