我也不知道,呵呵,你贴出来就占你光了
Imports System.Data.SqlClient
Dim backupdb As String
Dim backupdisk As String
Dim restoredb As String
Dim restoredisk As String
Dim cn As New SqlConnection("data source=.;database=master;integrated security=true;")
Dim cmdatabase As New SqlCommand
Dim SaveFileDialog As New SaveFileDialog
Dim openfiledialog As New openfiledialog
Dim cmBackup As New SqlCommand
Dim cmRestore As New SqlCommand
Dim kill As New SqlCommand
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'声明一个Result枚举变量,记录对话框的返回值,即判断单击了“打开”还是“取消”按钮
Dim Result As Windows.Forms.DialogResult
'设置文件筛选器
SaveFileDialog.Filter = "BAK格式(*.bak)|*.bak"
'设置对话框标题
SaveFileDialog.Title = "保存文件"
'打开对话框
Result = SaveFileDialog.ShowDialog()
'如果单击了“保存”按钮,并且filename不为空时,为变量@filename赋值
If Result = DialogResult.OK And Len(SaveFileDialog.FileName) > 0 Then
backupdisk = SaveFileDialog.FileName
Else
Exit Sub
End If
'为sqlCommand参数赋值
cmBackup.Parameters(1).Value = backupdb
cmBackup.Parameters(2).Value = backupdisk
'备份数据库
Try
'打开连接,执行存储过程
cn.Open()
cmBackup.ExecuteNonQuery()
MsgBox("恭喜你!备份成功!")
Catch ex As Exception
'异常处理
MsgBox(ex.ToString)
End Try
'关闭连接
cn.Close()
End Sub
Private Sub 数据维护_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label3.Visible = False
cn.Open()
cmdatabase.Connection = cn
cmdatabase.CommandText = "sp_helpdb"
cmdatabase.CommandType = CommandType.StoredProcedure
cmBackup.Connection = cn
cmBackup.CommandText = "[backupDATABASE]"
cmBackup.CommandType = CommandType.StoredProcedure
cmBackup.Parameters.Add("@RETURN_VALUE", SqlDbType.Int, 4)
cmBackup.Parameters("@RETURN_VALUE").Direction = ParameterDirection.ReturnValue
cmBackup.Parameters.Add("@db", SqlDbType.VarChar, 20)
cmBackup.Parameters.Add("@disk", SqlDbType.VarChar, 50)
cn.Close()
cn.Open()
cmdatabase.Connection = cn
cmdatabase.CommandText = "sp_helpdb"
cmdatabase.CommandType = CommandType.StoredProcedure
cmRestore.Connection = cn
cmRestore.CommandText = "[RESTOREDATABASE]"
cmRestore.CommandType = CommandType.StoredProcedure
cmRestore.Parameters.Add("@RETURN_VALUE", SqlDbType.Int, 4)
cmRestore.Parameters("@RETURN_VALUE").Direction = ParameterDirection.ReturnValue
cmRestore.Parameters.Add("@dbname", SqlDbType.VarChar, 20)
cmRestore.Parameters.Add("@disk", SqlDbType.VarChar, 50)
kill.Connection = cn
kill.CommandText = "[killspid]"
kill.CommandType = CommandType.StoredProcedure
kill.Parameters.Add("@RETURN_VALUE", SqlDbType.Int, 4)
kill.Parameters("@RETURN_VALUE").Direction = ParameterDirection.ReturnValue
kill.Parameters.Add("@dbname", SqlDbType.VarChar, 20)
'kill.Parameters.Add("@sql", SqlDbType.NVarChar, 500)
'kill.Parameters.Add("@spid", SqlDbType.Int, 4)
cn.Close()
restoredb = Label3.Text
backupdb = Label3.Text
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Result As Windows.Forms.DialogResult
'设置文件筛选器
openfiledialog.Filter = "BAK格式(*.bak)|*.bak"
'设置对话框标题
openfiledialog.Title = "打开文件"
'打开对话框
Result = openfiledialog.ShowDialog()
'如果单击了“打开”按钮,并且filename不为空时,为变量@filename赋值
If Result = DialogResult.OK And Len(openfiledialog.FileName) > 0 Then
restoredisk = openfiledialog.FileName
Else
Exit Sub
End If
'为sqlCommand参数赋值
cmRestore.Parameters(1).Value = restoredb
cmRestore.Parameters(2).Value = restoredisk
kill.Parameters(1).Value = restoredb
'还原数据库
Try
cn.Open()
kill.ExecuteNonQuery()
cmRestore.ExecuteNonQuery()
Label1.Text = "恢复成功"
Catch ex As Exception
'异常处理
Label1.Text = "恢复失败" + ex.ToString()
End Try
'关闭连接
cn.Close()
End Sub