本文介绍了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

推荐答案


这篇关于sqlserver db由vb.net恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 22:31