sql BACKUP DATABASE可以备份与RESTORE DATABASE 恢复本地sql数据库文件,那么是否可以代码备份与恢复远程服务器数据库文件到本地sql数据库呢?
备份本地SQL数据库数据:
Dim mc As String = "党建"
Dim bf As String = "D:\bak\"& mc & format(Date.now,"yyyyMMddHHmmss") & ".bak"
Dim Result As DialogResult
Result = MessageBox.Show("是否本机上的数据库?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Result = DialogResult.Yes Then
Dim cmd As New SQL Command
cmd.Connection Name = "党建"
cmd.CommandText = "use master;BACKUP DATABASE " & mc & " TO DISK = '" & bf & "' With FORMAT,CHECKSUM;"
cmd.ExecuteNonQuery()
MessageBox.Show("备份完毕!")
End If
从本地备份文件恢复SQL数据库数据:
Dim dlg As New openFileDialog '定义一个新的SaveFileDialog
dlg.Filter= "备份文件|*.bak" '设置筛选器
Dim mc As String = "党建"
If dlg.ShowDialog = DialogResult.Ok Then '如果用户单击了确定按钮
Dim cmd As New SQL Command
cmd.Connection Name = "党建"
cmd.Comma ndText = "use master;RESTORE DATABASE " & mc & " FROM DISK= '" & dlg.FileName & "' "
cmd.ExecuteNonQuery()
MessageBox.Show("恢复完毕!")
End If
[此贴子已经被作者于2021/5/16 0:42:42编辑过]