Re: "exclusive access could not be obtained.." while restoring

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Jim Young (thorium48_at_hotmail.com)
Date: 06/01/04

  • Next message: newbie: "Re: "exclusive access could not be obtained.." while restoring"
    Date: Tue, 1 Jun 2004 13:32:52 -0700
    
    

    What is probably happening is that you're application user is defaulting to
    the database that your are attemping to restore. You can't restore a
    database that is in use. You will need to find some way of changing the
    database in use to the master database before performing the restore. One
    way is to change the Initial Catalog value of your connection string to be
    the master database.

    Jim

    "newbie" <anonymous@discussions.microsoft.com> wrote in message
    news:7D202145-B997-4690-AFBB-CC02F3242106@microsoft.com...
    > Hello,
    >
    > I am using MSDE with my application and providing our users UI to
    backup/restore the database. My app has just 1 database and 1 login mapped
    to 1 user (MyAppUser). Backup and restore functionality is using inline sql
    commands. Backup works fine with something like this:
    >
    > Private Sub Backup()
    > Dim cn As New SqlConnection(MyConnectionString)
    >
    > Try
    > cn.Open()
    > Dim cm As New SqlCommand
    > With cm
    > .Connection = cn
    > .CommandType = CommandType.Text
    >
    > .CommandText = "BACKUP DATABASE MyDB TO DISK =
    'D:\Backup\a.bak' WITH INIT"
    > .ExecuteNonQuery()
    > End With
    > MsgBox("Database backed up successfully!")
    > Catch ex As Exception
    > MsgBox(ex.Message)
    > Finally
    > cn.Close()
    > End Try
    > End Sub
    >
    > but when I do restore using something like this:
    > Private Sub Restore()
    > Dim cn As New SqlConnection(MyConnectionString)
    >
    > Try
    > cn.Open()
    > Dim cm As New SqlCommand
    >
    > With cm
    > .Connection = cn
    > .CommandType = CommandType.Text
    > .CommandText = "RESTORE DATABASE MyDB FROM DISK =
    'D:\Backup\a.bak' WITH RECOVERY"
    > .ExecuteNonQuery()
    > End With
    > MsgBox("Database restored successfully!")
    > Catch ex As Exception
    > MsgBox(ex.Message)
    > Finally
    > cn.Close()
    > End Try
    > End Sub
    >
    > I get the "exclusive access could not be obtained.. .. database is in use"
    error message.
    > I did "Use Master" in query analyser and ran the same restore sql command
    and it worked fine. I do not know how to use "Use master" here in ado.net.
    I know it has something to do with sp_Who but not sure how the syntax will
    fit in.
    >
    > Note MyConnectionString is something like:
    > "data source=(local)\MyCompany;initial catalog=MyDB;User ID = MyAppUser ;
    Password = MyPassword"
    >
    > Please help. What should I do so that this works in my vb.net app.
    >


  • Next message: newbie: "Re: "exclusive access could not be obtained.." while restoring"

    Relevant Pages

    • RE: How do I restore from mdf and ldf files?
      ... Tasks -> Backup Database ... Tasks -> Restore Database ... Windows 2003 Server with Latest Service Pack ... Pre-requisites for Sharepoint Backup and Restore: ...
      (microsoft.public.sharepoint.portalserver)
    • Re: Restoring SQL Server Backups
      ... ORIGINAL BACKUP & RESTORE (MSDE Database) ... occurrenceof cachestore flush for the 'Object Plans' cachestore (part ...
      (microsoft.public.sqlserver.server)
    • Re: Restoring SQL Server Backups
      ... ORIGINAL BACKUP & RESTORE (MSDE Database) ... occurrenceof cachestore flush for the 'Object Plans' cachestore (part ...
      (microsoft.public.sqlserver.server)
    • Re: POINT IN TIME RESTORE
      ... This is what you should have done in order to do the restore as you wish: ... > 3- erase data ... > 5- restore database with norecovery ... The BACKUP DATABAE can of course be at an earlier point in time, ...
      (microsoft.public.sqlserver.server)
    • Re: Backend Exchange migrate to New Hardware (Server)
      ... Exchange mailbox stores and Exchange public folder stores. ... When you use Backup to restore Exchange databases, ... Storage Engine to restore Exchange database files and their ...
      (microsoft.public.exchange.setup)