Exforsys

Online 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 ...


Go Back   Exforsys > Programming Talk > Microsoft .NET

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-11-2006, 05: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
  #2 (permalink)  
Old 10-11-2006, 07:33 AM
Senior Member
 
Join Date: Mar 2005
Posts: 910
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

Thread Tools

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 09:37 AM
SQL database back-up !!!! Kartik SQL Server 2005 Tutorials 6 05-23-2005 02:48 PM
hierarchail & network kisshug Oracle Tutorials 2 05-01-2005 05:17 PM
SQL SERVER CE NareshShroff Microsoft .NET Tutorials and Articles 0 08-18-2004 05:32 AM
Migrating Oracle Databases to SQL Server 2000 sanereddy SQL Server 2005 Tutorials 0 07-05-2004 08:37 AM


All times are GMT -4. The time now is 01:16 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.