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