SQL Server database restoration experience

Source: Internet
Author: User

In the past few days, the restoration function of the ms SQL database is used. One problem is that the current user is using the database and cannot recover it. I checked some posts, mainly through the stored procedure. A few days ago, I found a class that can be restored, and then I asked the students to solve the problem with their methods. The main principle is to turn off the current user by another user, and then use the new user for recovery. The new user has higher permissions.

The recovery code is as follows:

If f = "" Then 'f is the backup file name
Msgbox "select backup file", vbokonly + vbinformation, "prompt"
Else

CNN. Close 'Shut down the current user process first
Cnn2.open "provider = sqloledb.1; Integrated Security = sspi; persist Security info = false; Data Source = (local)" 'open a new user process. Use Windows to verify logon, you can also use another user, just change the connection string.



SQL = "select spid from Master... sysprocesses where dbid = db_id ('devic')" 'queries users who are using the device Database
Set rst = cnn2.execute (SQL)

While RST. EOF = false
SQL = "kill" & RST. Fields (0) 'Close the user process in use
Cnn2.execute SQL 'note that cnn2 is executed by a new process.
RST. movenext
Wend
RST. Close

SQL = "Restore database device from disk = '" & F & "'" 'Restore database command
Cnn2.execute SQL

Cnn2.close
Set cnn2 = nothing 'cnn2 fulfill its mission and completely release it

CNN. open' and then open the original connection
Msgbox "recovery successful", vbokonly + vbinformation, "prompt"
End if

This method avoids the inconvenience caused by stopping the Database Service and ensures that the original connection can continue to be used. The overall feeling is more convenient.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.