sqlserver db由vb.net恢复

问题描述:

我可以动态创建和恢复数据库但是恢复时间我有错误

错误 - >

i ma creating and restoring database dynamically but restoring time i got error
Error-->

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

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





class name

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

我认为在您的应用程序中,您已连接到您的数据库,如ABC,并且在您的代码中,您正在尝试恢复某些数据库备份在同一个数据库ABC上,所以它不可能,因为你的连接或会话处于活动模式到ABC数据库。您可以通过关闭与该数据库的所有连接进行检查,并尝试再次运行您的代码并检查天气是否正常工作。
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.