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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
SenthilkumarPosted May 16, 2012, 1:59 AM
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.
Satyapriya NayakPosted May 15, 2012, 1:08 PM
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
Pravinkumar BirajdarPosted May 16, 2012, 10:23 AM
Kunal VaishyaPosted May 15, 2012, 1:23 PM
try this
Kunal VaishyaPosted May 15, 2012, 1:19 PM
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