Hi Harsh,
To backup the database using VB.NET
1) Connect to the database thatyou use in the application, or connect to themaster database. The master database is recommended.
2)Backup thedatabase using the SQL statement.
below is a sample code for your information. Code Snippet Sub backupDb()
Dim mysqlcomm As SqlClient.SqlCommand
Dim strsql As String
Dim strbak As String
Try
If backupDir <> Nothing Then
If Not System.IO.Directory.Exists(backupDir) Then
System.IO.Directory.CreateDirectory(backupDir)
End If
Else
If Not System.IO.Directory.Exists(Application.StartupPath + "\backup") Then
System.IO.Directory.CreateDirectory(Application.StartupPath + "\backup")
End If
backupDir = Application.StartupPath + "\backup"
End If
strbak = backupDir
strsql = "backup database myDataBase to disk='" + strbak + "\myDataBase.bak' with name='myDataBase backup all',description='Full Backup Of myDataBase'" 'SQL statement for backup
'strsql = "RESTORE DATABASE { database_name | @database_name_var } [ FROM < backup_device > [ ,...n ] ] " 'SQL statement for restoring
mysqlconn.Open()
mysqlcomm = New SqlClient.SqlCommand(strsql, mysqlconn)
mysqlcomm.ExecuteNonQuery()
mysqlconn.Close()
Catch ex As Exception
If Not mysqlconn.State.Closed = ConnectionState.Closed Then
mysqlconn.Close()
End If
Throw ex
End Try
End Sub
Sub open_Masterconn()
Dim sqlconnStr As String
Try
sqlconnStr = "data source = " + DbHost + " ; initial catalog = master; user id = " + connUser + "; password = " + connpwd
mysqlconn = New SqlClient.SqlConnection(sqlconnStr)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Hope this helps.
Regards |