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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send command to DB without logging on to DB

Status
Not open for further replies.

DroopyA

Programmer
Aug 5, 2003
52
US
Ok, I've been at this for about 3 days now and I'm sorry if my constant "restore" questions are getting old but I've finally figured out exactly what's going wrong and I think I'm offically SOL.

I need to (through code since none of my clients will have SQL Server) Backup and Restore my programs SQL database at the push of a button. Backup works just fine, but Restore keeps giving me an error saying that it can't restore because people are logged on to the database. The problem is, there is only one person logged on to the database I'm trying to backup and that's me sending the "RESTORE" command to the database. So how can I tell MSDE/SQL to Restore the database, without first connecting to the database?

Here is some test code I made:

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[/code]
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 Backup
m_cn.Execute ("RESTORE DATABASE [TagLinkSQLCS] FROM DISK = N'C:\Program Files\TagLink\Data\TagLinkBackup.dat' WITH FILE = 1, 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 text is where I restore the database. The Restore syntax is correct but SQL says I'm not allowed to Restore because m_cn is connected to the database. How am I suppose to tell SQL to restore, if I can't connect to SQL to send the command?

Thank you
-N473
 

First off you do not need to restore the database after a backup. The only time you will need to restore the database is when there is an error in the database or it becomes corrupt. If for some reason you need to do a restore and you have admin rights you can log into the service and send your restore command, or you can log into the master databse and send your restore command.

Good Luck

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top