如何在SQL Server中恢复数据

 更新时间:2016年11月25日 16:44  点击:2299
在SQL server 中恢复数据的几种办法:
1.自然就是 backup 的 恢复方法 backup 这种方法是最安全,最可靠的方法操作起来也很简单,只要在 sql server 的 enterprise manager中选择 restore 就可以了。
用T-SQL 也可以完成:
RESTORE DATABASE test FROM DISK = ’c:mssql7ackup est.bak’
当然这是用的 文件恢复,如果是 设备恢复 自然也是可以的。
2.可是有的时候,SQL server 是在我们毫无准备的情况下瘫痪的,有的时候是伴随者 NT 的瘫痪而引起的,(这个时候,豆腐想 墙上 Gates 的画像有仍了几个 西红柿),怎么办呢?这个时候就只有采用 sql server 的 t-sql 中提供的系统存储过程了:
sp_atach_db
在这里举一个简单的例子:
sp_attach_db @dbname = N’pubs’,
@filename1 = N’c:mssql7datapubs.mdf’,
@filename2 = N’c:mssql7datapubs_log.ldf’
这种方法应该说是有很高的成功率的,不过在 实践中 豆腐听说 这样后,数据库成为只读的,那也没有办法了。如果只有 mdf 没有 ldf 文件的话,可以使用 sp_attach_single_file
sp_attach_single_file_db @dbname = ’pubs’, @physname = ’c:mssql7datapubs.mdf’
这个方法本来是 用来 对 sp_deatach 的操作的反操作,不过直接使用也还是可以成功的。
如果您在Access数据库、Access项目中删除数据或对象,可能会产生碎片并导致磁盘空间使用效率的降低。同时,数据库文件的大小并未减小,而是不断的增大,直至您的硬盘没有空间。有没有好的处理方法呢?其实,在Access中可以对数据库进行压缩优化以提升Access数据库和Access项目的性能,这样的压缩处理的实质是复制该文件,并重新组织文件在磁盘上的存储方式。但是,在Access项目中进行这样的压缩不会影响到数据库对象(例如表或视图),因为它们是存储在Microsoft SQL Server数据库中而不是在Access项目本身中。同样,这样的压缩也不会影响到Access项目中的自动编号。在Access数据库中,如果已经从表的末尾删除了记录,压缩该数据库是就会重新设置自动编号值。添加的下一个记录的自动编号值将会比表中没有删除的最后记录的自动编号值大一。
下面介绍如何在VB中用一个CompactJetDatabase过程实现对Access数据库文件的压缩处理,在这个过程中有一个可选参数,就是在压缩前你是否需要把原有的数据库文件备份到临时目录(True或False)。我用此办法使21.6MB的数据库压缩到仅仅300KB。
'这些代码可放在模块中,在其他窗体也使用
Public Declare Function GetTempPath Lib "kernel32" Alias _
"GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Public Const MAX_PATH = 260
Public Sub CompactJetDatabase(Location As String, Optional BackupOriginal As Boolean = True)
On Error GoTo CompactErr
Dim strBackupFile As String
Dim strTempFile As String
'检查数据库文件是否存在
If Len(Dir(Location)) Then
'如果需要备份就执行备份
If BackupOriginal = True Then
strBackupFile = GetTemporaryPath & "backup.mdb"
If Len(Dir(strBackupFile)) Then Kill strBackupFile
FileCopy Location, strBackupFile
End If
'创建临时文件名
strTempFile = GetTemporaryPath & "temp.mdb"
If Len(Dir(strTempFile)) Then Kill strTempFile
'通过DBEngine压缩数据库文件
DBEngine.CompactDatabase Location, strTempFile
'删除原来的数据库文件
Kill Location
'拷贝刚刚压缩过临时数据库文件至原来位置
FileCopy strTempFile, Location
这里有几个关于SQL SERVER的问题想向您请教(服务器端脚本编程):
(1)如何在SQL SERVER中用SQL语句获得当前的数据库名称?
(2)如何在定义一个全局变量而不是局部变量,因为我在这里定义了
一个循环,在这个循环里有对一个临时表加字段和对临时表插入数据
的操作。但这两个操作必须放在不同的批次里.(需要用GO语句分开)。
这时控制循环的变量参数就不起作用了,因为变量参数只能在一个
批次里起作用,到下一个批次就要重新定义了。真头疼!能不能定义
全局变量,让它在所有批次中都起作用?
这两个问题,其实并不具有一定的普遍性,但是解决这两个问题的 思路和方法 却有一定的 普遍性,所以我专门把这两个问题拿出来 讲一下
(1)首先 这个当前 所在 DataBase 肯定需要用到 系统存储过程 这一点是 十分肯定的,我自然想到了sp_who 执行后,列出了当前所有的 系统进程的 所有信息,我一看 有 Runnable,有 Slepping 肯定应该是 runnable 了,一看 他的 spid 想起来了,有个系统的 全局变量@@SPID,先试一下,sp_who @@spid 哈哈 成功了这个问题的解决之道 是 一定要熟悉 系统的存储过程和系统全局变量,并且要善于观察和思考
(2) 至于 第二个问题,则纯粹是一种 t-sql 编程的思路问题,首先我不赞同用一个规则去判断两个 存储过程,因为这不符和 t-SQL 编程的规范,但是既然问题出来了,就要想办法 解决,我们知道在 两个批次里 是不可能共巷数据的,所以 我想到了 cursor 游标,最终 经过测试,也成功了。

from: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=4&txtCodeId=6518
Connect/Read Remote SQL server Using PWS in win 98
I had to test Microsoft's Personal Webserver (PWS) in win 98 to access Remote SQL server 7.0
installed in a NT server. The clients to win 98 had LAN connections. Easy one , don't vote, have fun with
PWS. In fact I tested my connection with this script, before I created an out of process server demo with
VB.





code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!

Terms of Agreement:
By using this code, you agree to the following terms...
1) You may use this code in your own programs (and may compile it into a program and distribute it in
compiled format for langauges that allow it) freely and with no charge.
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the
original author. Failure to do so is a violation of copyright laws.
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the code or
code's description.

'**************************************
' Name: Connect/Read Remote SQL server U
' sing PWS in win 98
' Description:I had to test Microsoft's
' Personal Webserver (PWS) in win 98 to ac
' cess Remote SQL server 7.0 installed in
' a NT server. The clients to win 98 had L
' AN connections. Easy one , don't vote, h
' ave fun with PWS. In fact I tested my co
' nnection with this script, before I crea
' ted an out of process server demo with V
' B.
' By: Manas Mukherjee
'
在SQL Server中提供了这种恢复方式的存储过程。
一个数据库包括两个文件,mdf数据库文件和ldf日志文件
1.sp_attach_db [@dbname =] 'dbname',[@filename1 =] 'filename_n'
给系统添加一个数据库,在dbname指定数据库名称,filename_n指定数据库的文件和日志文件。比如我有一个ji的库,停止SQL Server服务备份ji_data.mdf,ji_log.ldf,启动SQL server,删除掉这个库,然后再把这两上文件拷到sql server DATA目录中,在Query Analyzer中执行如下语句:
EXEC sp_attach_db @dbname = N'ji',
@filename1 = N'd:mssql7dataji_data.mdf',
@filename2 = N'd:mssql7dataji_log.ldf'
就会把这个库加入到SQL Server Group中.

[!--infotagslink--]

相关文章