Exforsys

Free Training

How to restore SQL Database

This is a discussion on How to restore SQL Database within the Microsoft .NET forums, part of the Programming Talk category; I have a problem of restoring my SQL database using VB.NET. I got an error which says that the database ...

Go Back   Exforsys > Programming Talk > Microsoft .NET

Exforsys.com

  #1 (permalink)  
Old 10-11-2006, 04:55 AM
Junior Member
 
Join Date: Jul 2006
Posts: 1
fikmesf is on a distinguished road
How to restore SQL Database

I have a problem of restoring my SQL database using VB.NET. I got an error which says that the database is in use.

I used the following code please help me to resolve the problem by providing me a code that disconnect and connect the database

Thank you
Mesfin

Sub RestoreDBFromFile(ByVal ServerName As String, ByVal DBName As String, ByVal BackupToRestore As String, byval usedatabase as String)
Dim oServer As SQLDMO.SQLServer
Dim oRestore As SQLDMO.Restore
On Error GoTo Handler
'simple err checking
If ServerName = "" Or DBName = "" Or BackupToRestore = "" Then
MsgBox("You MUST provide server name, database name, and the name of the bak file you want to restore", vbInformation + vbOKOnly, "Error")
Exit Sub
End If
'open connection to server
oServer = New SQLDMO.SQLServer
With oServer
.LoginSecure = True
.Connect(ServerName)
'.AttachDB("Master", "master.mdf")
End With

'also need a restore object
oRestore = New SQLDMO.Restore
'use the 'with' construct to minimize property lookups
With oRestore

'this is where your backup will be restored to
.Database = DBName
'same as EM or TSQL, you can restore database, file, or log, here we're going to
'use database
.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database
'this is the "force restore over existing database" option
.ReplaceDatabase = True
'this does a restore from a file instead of a device - note that we're still
'restoring a database, NOT a file group
.Files = BackupToRestore
'do it
.SQLRestore(oServer)
End With
'standard clean up
oRestore = Nothing
oServer.DisConnect()
oServer = Nothing
Exit Sub
Handler:
If MsgBox(Err.Description & ". Would you like to continue?", vbInformation + vbYesNo) = vbYes Then
Resume Next
End If
End Sub
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-11-2006, 06:33 AM
Senior Member
 
Join Date: Mar 2005
Posts: 916
techguru is on a distinguished road
You might has users connected to the database.. first you need clear those sessions before the restore.

are you trying to restore master db..?

try with some otehr database .. it should work...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Database Tuning Advisor in SQL Server 2005 Happy Harry SQL Server 2 04-18-2006 08:37 AM
SQL database back-up !!!! Kartik SQL Server 2005 Tutorials 6 05-23-2005 01:48 PM
hierarchail & network kisshug Oracle Tutorials 2 05-01-2005 04:17 PM
SQL SERVER CE NareshShroff Microsoft .NET Tutorials and Articles 0 08-18-2004 04:32 AM
Migrating Oracle Databases to SQL Server 2000 sanereddy SQL Server 2005 Tutorials 0 07-05-2004 07:37 AM


All times are GMT -4. The time now is 09:22 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
Copyright 2004 - 2009 Exforsys Inc. All rights reserved.