سلام
من برای برگرداندن دیتابیس از این روش استفاده کردم (Console Application):
Sub ExecuteQuery(Query As String)
Try
Dim Server As String = ""
Dim login As String = ""
Dim password As String = ""
Dim ConnectionString As String = ""
Dim Value As String = ""
Console.WriteLine()
Console.ForegroundColor = ConsoleColor.Yellow
Console.WriteLine("{0} Connect to New Server: ", vbTab)
Console.WriteLine()
Console.ForegroundColor = ConsoleColor.DarkGreen
value = " Server: "
FillName(value, 17)
Console.Write(value)
Console.ForegroundColor = ConsoleColor.Gray
value = Console.ReadLine
If Value.Length = 0 Then Exit Sub
Server = value.Trim
Console.WriteLine()
Console.ForegroundColor = ConsoleColor.DarkGreen
Value = " Login(User ID): "
FillName(value, 17)
Console.Write(value)
Console.ForegroundColor = ConsoleColor.Gray
Console.WriteLine("sa")
login = "sa"
Console.ForegroundColor = ConsoleColor.DarkGreen
Value = "'sa' Password: "
FillName(value, 17)
Console.Write(value)
Console.BackgroundColor = ConsoleColor.Black
Console.ForegroundColor = ConsoleColor.Black
value = Console.ReadLine
password = value.Trim
Console.ResetColor()
Dim connection As SqlConnection
ConnectionString = NewConnectionString(Server, login, password, "master")
connection = New SqlConnection(ConnectionString)
connection.Open()
Dim command As SqlCommand
command = connection.CreateCommand()
command.CommandText = Query
command.ExecuteNonQuery()
Catch ex As Exception
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("{0} {1}! {2}", vbTab, ex.Message.Trim, Query)
End Try
End Sub
Function NewConnectionString(Server As String, login As String, password As String, Optional Database As String = "Projects") As String
Return String.Format("Data Source={0};Initial Catalog={3};User ID={1};Password={2}", Server, login, password, Database)
End Function
Shared Sub Backup()
ExecuteQuery(String.Format("BACKUP DATABASE Projects TO DISK='{0}'",
IO.Path.GetFullPath(".\Projects.BAK")))
End Sub
Shared Sub Restore()
ExecuteQuery(String.Format("RESTORE DATABASE Projects FROM DISK='{0}'",
IO.Path.GetFullPath(".\Projects.BAK")))
End Sub
ولی موقع Restore این خطا رو بر می گردونه که دیتابیس در حال استفاده است....
With ProjectsAdapter.Connection
.Close()
End With
AdapterManager.Connection.Close()
Data.Restore()
Screenshot 2024-10-17 125258.jpg