Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i ma creating and restoring database dynamically but restoring time i got error
Error-->
SQL
RESTORE cannot process database 'Dbname' because it is in use by this session. It is recommended that the master database be used when performing this operation.

RESTORE DATABASE is terminating abnormally.


following my code

VB
Dim sqlnewDbcrecon As SqlConnection = New SqlConnection("Data Source=" & cbServers.SelectedItem.ToString & "; Initial Catalog=" & NewFDb & "; user Id=; password=;")
           Dim rptpath As String
           If ApplicationDeployment.IsNetworkDeployed = True Then
               rptpath = AppDomain.CurrentDomain.BaseDirectory.ToString & "Db\Pharma"
           Else
               rptpath = AppDomain.CurrentDomain.BaseDirectory.ToString
               rptpath = rptpath.Substring(0, rptpath.Length - 10) + "Db\Pharma"
           End If

           FYear = TxtreferDbname.Text & Year(dtpstartfinyear.Text) & Year(dtpEndfinyear.Text)
           Dim Splfyear() As String = FYear.Split("20")

           NewFDb = TxtreferDbname.Text + Splfyear(1).TrimStart("0") + Splfyear(2).TrimStart("0")
                       Dim sqlDbcrecon As SqlConnection = New SqlConnection("Data Source=" & cbServers.SelectedItem.ToString & "; user Id=; password=;")

             Cls.cmdNewfire("CREATE DATABASE " + NewFDb + " ON (NAME = Sales_dat,FILENAME = '" + TxtDbPathname.Text + "\" & NewFDb & ".mdf',SIZE = 10,MAXSIZE = 50,FILEGROWTH = 5)LOG ON( NAME = '" & NewFDb & "_log', FILENAME = '" + TxtDbPathname.Text + "\" & NewFDb & ".ldf',SIZE = 5MB, MAXSIZE = 25MB,FILEGROWTH = 5MB)", sqlnewDbcrecon)
           Dim Oldbnam As String = "Pharma"
           Dim newdbname As String = NewFDb
           Dim sqlnewDbcreconn As SqlConnection = New SqlConnection("Data Source=" & cbServers.SelectedItem.ToString & "; Initial Catalog=" & NewFDb & "; user Id=; password=;")
           Cls.cmdNewfire("RESTORE DATABASE " & newdbname & " FROM DISK = N'D:\Manoj\" & Oldbnam & "' WITH FILE = 1, " &
                  "MOVE 'VBPharma' TO N'D:\Manoj\" & Oldbnam & ".mdf', " &
                  "MOVE 'VBPharma_log' TO N'D:\Manoj\" & Oldbnam & "_Log.ldf', Replace", sqlnewDbcreconn)




class name
VB
Public Function cmdNewfire(ByVal qry As String, ByVal sqlcon As SqlConnection) As String
       Dim sqlcmd As New SqlCommand(qry, sqlcon)
       sqlcon.Open()
       cmdNewfire = sqlcmd.ExecuteNonQuery
       sqlcon.Dispose()
       sqlcmd.Dispose()
   End Function
Posted
Updated 23-Dec-13 2:25am
v5
Comments
Maciej Los 23-Dec-13 8:27am    
Where do you execute above code: on sever or on client machine?

1 solution

I think in your application you are connected to your database like ABC and, in your code your are trying to restore some database backup over the same database ABC, so its not possible because your connection or session is in active mode to ABC database. you can check by closing all the connection to that database and try to run your code again and check weather its working or not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900