Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send Restore without being on Database? 1

Status
Not open for further replies.

DroopyA

Programmer
Aug 5, 2003
52
US
Some of you might have noticed my 4 posts in the past 2 days all about the same Restore problem but I've finally figured out what's going wrong. I'm trying to tell my SQL database to restore through VB code, thus requireing me to link to the database to tell it to execute. However, it's telling me that I can't restore because somebody is using the database. Well of course they are, it's me telling the DB to restore. How do I go around this? I have to connect to the database to tell it what to do, but when I connect to it, it can't restore because I'm logged in. My clients will not have SQL Server so this must be done through code. Any ideas? Heres my code:

Private Sub Command2_Click()
'N473 ******************************* N473
'N473 * Backup/Restore the Database * N473
'N473 ******************************* N473
Dim rsUsers As ADODB.Recordset
Dim m_cn As ADODB.Connection
Dim intResponse As Integer

On Error GoTo GeneralError

' Open the Connection
Set m_cn = New ADODB.Connection
m_cn.ConnectionString = "Provider = SQLOLEDB.1; Integrated Security=SSPI; Initial Catalog = TagLinkSQLCS; Data Source = " & txtComputerName.Text & ";Initial File Name = C:\PROGRA~1\TAGLINK\DATA\TagLinkSQL.mdf"
m_cn.CursorLocation = adUseClient
m_cn.Open

' Restore the Database
m_cn.Execute ("RESTORE DATABASE [TagLinkSQLCS] FROM DISK = N'C:\Program Files\TagLink\Data\TagLinkBackup.dat' WITH FILE = 12, NOUNLOAD , STATS = 10, RECOVERY")

MsgBox "Restore Completed successfully", vbOKOnly, "TESTING"
Exit Sub

GeneralError:
MsgBox Err.Description, vbOKOnly, "TESTING"

rsUsers.Close
m_cn.Close
Set rsUsers = Nothing
Set m_cn = Nothing
End Sub


The bold line gives me an error saying it can't restore because the database is in use. However I have to be connected to the database to "Execute" the RECOVER command. Now what?

Thank you so much for all of your help
-Nate
 
Restores should be executed while connected to the MASTER db. This is how I have implemented my restores. So, instead of:

Initial Catalog = TagLinkSQLCS;

Use

Initial Catalog = master;
 
I LOVE YOU!!!!!!!!! Thank you so much, I've been at this Backup/Restore thing for 3 days now. You are my hero.

Thank you Thank You Thank you
-Nate
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top