Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2003
    Posts
    19

    Unanswered: Restoring Database using SQL-DMO gives me an error on another DB Server

    Hi I am trying to restore a database using SQL-DMO. Restoring works on my development computer, but it does not work on my colleague's development computer. Although there are no users using the database in question, I get the following error message while attempting to restore the backup file:

    Exclusive access could not be obtained. Database is in use.

    Do I have to do anything in particular? Here is my snippet of code (VB.net). Thanks.



    Dim my_srv As SQLDMO._SQLServer = New SQLDMO.SQLServerClass
    my_srv.Connect(my_config.get_ServerName(), my_config.get_DBUserID(), my_config.get_Password)

    'COPY FILE TO SPECIFIC DIRECTORY ON SERVER
    Dim my_file As File
    my_file.Copy(Me.txtFilePath.Text, "\\" + my_config.get_ServerName + my_config.get_backupfolder_string + "tempcas.bak")

    Dim my_restore As SQLDMO.Restore = New SQLDMO.RestoreClass
    'my_restore.Devices = my_restore.Files
    my_restore.Action = SQLDMO_RESTORE_TYPE.SQLDMORestore_Database
    my_restore.Files = "[" & my_config.get_backupdrive_string & my_config.get_backupfolder_string & "tempcas.bak]"
    my_restore.Database = my_config.get_Database()
    my_restore.ReplaceDatabase = True
    my_restore.SQLRestore(my_srv)
    MsgBox("Database restored successfully.", MsgBoxStyle.Information)

    'DELETE TEMP FILE FROM SPECIFIC DIRECTORY ON SERVER
    my_file.Delete("\\" + my_config.get_ServerName + my_config.get_backupfolder_string + "tempcas.bak")

  2. #2
    Join Date
    Aug 2002
    Location
    Scotland
    Posts
    1,578
    Make sure you are not using the same database which using the DMO script.
    Try using MASTER database instead.
    --Satya SKJ
    Microsoft SQL Server MVP
    [IMG]http://sqlserver-qa.net/google_bart.gif[/IMG]

  3. #3
    Join Date
    Nov 2003
    Posts
    19
    Hi, sorry, I'm not following you. Could you elaborate more?

    Thanks!

  4. #4
    Join Date
    Feb 2002
    Posts
    2,232
    When you connect, what is the database you are connecting to ?

  5. #5
    Join Date
    Nov 2003
    Posts
    19
    I'm connecting to a database named CAS. Anyway the database name would vary on what I have set in an xml file. As you can see from my code, I am getting the database name from a class.

    I've found that it is because my query analyser is open and pointed to CAS, this causes a process to be created. I can see this process with a status RUNNABLE, dbName CAS and program name SQL Query Analyser when I run sp_who2. When I change query analyzer to point to another database, this process will disappear and I would be able to restore my CAS database.

    Is this what you were trying to tell me? Thanks for your replies.

    I am now concerned about what would happen if there are multiple users using the application.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •