熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

使用VB.net實現sql數據庫的備份與恢復代碼

2013-11-13 10:12:31  來源: .NET編程 

  Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick 備份數據庫 BACKUP DATABASE Test TO DISK=C:\Backup WITH Format Dim cn As SqlConnection Dim cm As SqlCommand cn = getDB() cm = New SqlCommand(BACKUP DATABASE JXbase TO DISK=C:\Backup WITH Format cn) cnOpen() cmExecuteNonQuery() MsgBox(備份成功) cnClose()

  End Sub

  恢復 Private Sub Button_Click(ByVal sender As SystemObject ByVal e As SystemEventArgs) Handles ButtonClick Dim cn As New SqlConnection Dim cn As New SqlConnection Dim mydr As SqlDataReader Dim str As String

  Dim tmpConnectionString As String = Data Source=JJK;Initial Catalog=master;Integrated Security=True cnConnectionString = tmpConnectionString cnConnectionString = tmpConnectionString cnOpen() cnOpen() Dim cm As SqlCommand = New SqlCommand(select spid from mastersysprocesses where dbid=db_id(JXbase) cn) mydr = cmExecuteReader()

  Dim cm As SqlCommand = New SqlCommand() cmConnection = cn

  While (mydrRead()) 殺死進程

  str = kill & mydr(spid)ToString() cmCommandText = str cmCommandType = CommandTypeText ApplicationDoEvents() cmExecuteNonQuery() 殺進程 End While mydrClose() cm = New SqlCommand(RESTORE DATABASE JXbase FROM DISK=C:\Backup cn) 恢復備份 cmExecuteNonQuery() MsgBox(恢復成功成功) cnClose() cnClose()

  End Sub


From:http://tw.wingwit.com/Article/program/net/201311/12901.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.