| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1462 人关注过本帖
标题:[求助]数据的备份与恢复
只看楼主 加入收藏
soccer
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2005-7-12
收藏
 问题点数:0 回复次数:12 
[求助]数据的备份与恢复

备份SQL数据库 和恢复的代码
请加上注释谢谢了~~

搜索更多相关主题的帖子: 数据 
2006-11-29 11:33
mm_
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-11-29
收藏
得分:0 
这个很容易啊 你做完数据库就点击数据库名称右键  备份数据库  点添加  选路径 下步下步就好了  还原的话你先开SQL 点数据库右键 选择还原数据库 相信你会怎么做了
2006-11-29 16:44
mm_
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-11-29
收藏
得分:0 
恢复不用代码的啊
2006-11-29 16:45
yong99
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-11-28
收藏
得分:0 
二楼说的对,恢复备份都很简单
2006-11-29 19:49
mm_
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2006-11-29
收藏
得分:0 
谢谢
2006-11-30 00:03
soccer
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2005-7-12
收藏
得分:0 

可能是我表达不清楚吧。我是要在VB。NET里备份SQL数据库 和恢复

2006-11-30 07:27
jordan51341
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-12-14
收藏
得分:0 
顶一下 我也遇到这个问题没有解决
2006-12-15 00:18
chen5322
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2006-5-25
收藏
得分:0 

在这论坛里面很多这方面的 收索下 我以前回复就有2个

2006-12-17 21:40
chen5322
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2006-5-25
收藏
得分:0 

Imports System.Data.SqlClient
Public Class 数据维护
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(数据维护))
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Button1
'
Me.Button1.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(192, Byte), CType(255, Byte))
Me.Button1.Location = New System.Drawing.Point(24, 120)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "备份"
'
'Button2
'
Me.Button2.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(192, Byte), CType(255, Byte))
Me.Button2.Location = New System.Drawing.Point(152, 120)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 1
Me.Button2.Text = "还原"
'
'Button3
'
Me.Button3.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(192, Byte), CType(255, Byte))
Me.Button3.Location = New System.Drawing.Point(296, 120)
Me.Button3.Name = "Button3"
Me.Button3.TabIndex = 2
Me.Button3.Text = "取消"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 24)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(368, 24)
Me.Label1.TabIndex = 3
Me.Label1.Text = "1.备份:将数据备份起来,以备不时之需。"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(24, 72)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(368, 23)
Me.Label2.TabIndex = 4
Me.Label2.Text = "2.将数据替换当前的数据!具一定危险性!强烈要求先进行备份!"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(264, 152)
Me.Label3.Name = "Label3"
Me.Label3.TabIndex = 5
Me.Label3.Text = "jxc"
'
'数据维护
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.BackColor = System.Drawing.SystemColors.ActiveCaptionText
Me.ClientSize = New System.Drawing.Size(416, 166)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.Name = "数据维护"
Me.Text = "数据维护"
Me.ResumeLayout(False)

End Sub

#End Region
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
Dim conStr1 As New SqlClient.SqlConnection("data source=.;database=master;integrated security=true;")

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

Try
Dim cmd As New SqlCommand("if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[RESTOREDATABASE]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[RESTOREDATABASE]", conStr1)
'判断是否存在名字为“RESTOREDATABASE”的存储过程 如果存在就删除
Dim cmd2 As New SqlCommand("if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[backupDATABASE]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[backupDATABASE]", conStr1)
'判断是否存在名字为“backupDATABASE”的存储过程 如果存在就删除
Dim cmd3 As New SqlCommand("if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[killspid]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[killspid]", conStr1)
'判断是否存在名字为“killspid”的存储过程 如果存在就删除
Dim cmd1 As New SqlCommand("CREATE PROC RESTOREDATABASE(@disk varchar(50),@dbname varchar(20)) AS RESTORE DATABASE @dbname FROM DISK=@disk", conStr1)
'定义创建还原存储过程
Dim cmd4 As New SqlCommand("create PROC backupDATABASE (@disk varchar(50),@db varchar(20)) AS backup DATABASE @db to DISK=@disk", conStr1)
'定义创建备份存储过程
Dim cmd5 As New SqlCommand("create proc killspid (@dbname varchar(20)) as begin declare @sql nvarchar(500) declare @spid int set @sql='declare getspid cursor for select spid from sysprocesses where dbid=db_id('''+@dbname+''')' exec (@sql) open getspid fetch next from getspid into @spid while @@fetch_status<>-1 begin exec('kill '+@spid) fetch next from getspid into @spid End close getspid deallocate getspid End ", conStr1)
'定义创建杀进程存储过程
'定义创建数据库存储过程
conStr1.Open() '打开连接
cmd.ExecuteNonQuery() '执行COMMAND语句
cmd2.ExecuteNonQuery() '执行COMMAND语句
cmd3.ExecuteNonQuery() '执行COMMAND语句
cmd1.ExecuteNonQuery() '执行COMMAND语句
cmd4.ExecuteNonQuery() '执行COMMAND语句
cmd5.ExecuteNonQuery() '执行COMMAND语句

Me.Hide()
conStr1.Close() '关闭连接
Catch ex As Exception
MsgBox(ex.ToString)
End Try

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
End Class
create PROC backupDATABASE (@disk varchar(50),@db varchar(20)) AS backup DATABASE @db to DISK=@disk
GO
create proc killspid (@dbname varchar(20)) as begin declare @sql nvarchar(500) declare @spid int set @sql='declare getspid cursor for select spid from sysprocesses where dbid=db_id('''+@dbname+''')' exec (@sql) open getspid fetch next from getspid into @spid while @@fetch_status<>-1 begin exec('kill '+@spid) fetch next from getspid into @spid End close getspid deallocate getspid End
GO
CREATE PROC RESTOREDATABASE(@disk varchar(50),@dbname varchar(20)) AS RESTORE DATABASE @dbname FROM DISK=@disk
GO

在数据库MASTER数据库创建这3个存储过程就可以了

2006-12-17 21:44
Viviwei
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:344
专家分:0
注 册:2006-4-11
收藏
得分:0 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsave.Click
Try
Dim obackup As SQLDMO.Backup
gDatabaseName = cmbDatabaseName.Text
obackup = New SQLDMO.Backup
obackupevent = obackup
obackup.Database = gDatabaseName
gBkupRstrFileName = txtDataFileName.Text
obackup.Files = gBkupRstrFileName
'删除原来的文件
If Len(Dir(gBkupRstrFileName)) > 0 Then
Kill((gBkupRstrFileName))
End If
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
'备份数据库
obackup.SQLBackup(gSQLServer)
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
obackupevent = Nothing
obackup = Nothing
MsgBox("备份完成.", MsgBoxStyle.Information)
Exit Sub
Catch ex As Exception
MsgBox("错误:" & Err.Description, MsgBoxStyle.Exclamation)
End Try
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdresume.Click
Try
Dim oRestore As SQLDMO.Restore
Dim Msg As String
Dim Response As String
gDatabaseName = cmbDatabaseName.Text
oRestore = New SQLDMO.Restore
orestoreevent = oRestore
oRestore.Database = gDatabaseName
gBkupRstrFileName = txtDataFileName.Text
oRestore.Files = gBkupRstrFileName
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
'恢复数据库
oRestore.SQLRestore(gSQLServer)
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
orestoreevent = Nothing
oRestore = Nothing
MsgBox("恢复完成.", MsgBoxStyle.Information)
Exit Sub
Catch ex As Exception
MsgBox("错误:" & Err.Description, MsgBoxStyle.Exclamation)
End Try
End Sub


爱上思考,爱上编程,爱上.net!
2006-12-18 18:24
快速回复:[求助]数据的备份与恢复
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018123 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved