Implementation of backup and recovery of SQL database in vb.net

Source: Internet
Author: User
Tags format end sql net backup

Private Sub button3_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button3.click ' Back Up database ' backup DATABASE Test to disk= ' C:\Backup ' with Format Dim cn As SqlConnection Dim cm as SqlCommand cn = Getdb () cm = New Sqlcomma nd ("BACKUP DATABASE jxbase to disk= ' C:\Backup ' with Format", CN) CN. Open () cm. ExecuteNonQuery () MsgBox ("Backup succeeded") CN. Close ()

End Sub

' Restore Private Sub button6_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button6.click Dim cn as New SqlConnection Dim Cn1 As New SqlConnection Dim mydr As SqlDataReader Dim str As String

Dim tmpconnectionstring as String = "Data source=jjk;initial catalog=master;integrated Security=true" CN. ConnectionString = tmpconnectionstring cn1. ConnectionString = Tmpconnectionstring CN. Open () cn1. Open () Dim cm as SqlCommand = New SqlCommand ("select spid from Master.") sysprocesses where dbid=db_id (' jxbase '), cn) mydr = cm. ExecuteReader ()

Dim CM1 as SqlCommand = New SqlCommand () cm1. Connection = Cn1

while (myDR. Read ()) ' Kill process

str = "Kill" & myDR ("spid"). ToString () Cm1.commandtext = str Cm1.commandtype = CommandType.Text application.doevents () cm1. ExecuteNonQuery () ' Kill process End while myDR. Close () cm = New SqlCommand ("Restore DATABASE jxbase from disk= ' C:\Backup '", CN) ' restores backup cm. ExecuteNonQuery () MsgBox ("Restore successful Success") CN. Close () cn1. Close ()

End Sub



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.