hola ah todos miren taba restaurando mi base de datos desde mi aplicacion VB2005 y me genera un Error que no puedo Solucionarlo el Error es: No se pudo obtener acceso exlusivo porque la base de datos esta en uso Fin anomalo de RESTORE DATABASE
mostrare una parte de mi aplicacion lo que toy haciendo lo que si no tengo problema es al guardar el backu pero para restaurar ahi ta el chongo :
mi clase clsconexion
Imports System.Data.SqlClient
Public Class clsConexion
Public cadenaconexion As New SqlConnection
Public Function conectar(ByVal server As String, ByVal DB As String, ByVal usuario As String, ByVal clave As String) As Boolean
Try
cadenaconexion.ConnectionString = "Data Source=.;Initial Catalog=BDBIBLIOTECA;Integrated Security=True"
cadenaconexion.Close()
Catch ex As Exception
MessageBox.Show(ex.Message.ToString())
cadenaconexion.Close()
End Try
End Function
Public Function consultaBD(ByVal sentenciaSQL As String) As DataTable
cadenaconexion.Open()
Dim consulta As New SqlCommand(sentenciaSQL, cadenaconexion)
Dim daBD As New SqlDataAdapter(consulta)
Dim dtBD As New DataTable
daBD.Fill(dtBD)
consultaBD = dtBD
cadenaconexion.Close()
End Function
End Class
'el Programa
Private Sub FrmPrincipal_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
enlace.conectar("", "", "", "")
End Sub
Private Sub RESTAURARToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RESTAURARToolStripMenuItem.Click
Dim cadena As String
If (sfdrestaurar.ShowDialog() = Windows.Forms.DialogResult.OK) Then
cadena = sfdrestaurar.FileName
If restaurar_basededatos(cadena) = True Then
MessageBox.Show("Base de Datos Restaurada", "Mensaje", MessageBoxButtons.OK)
End If
End If
End Sub
Private Function restaurar_basededatos(ByVal cadena As String) As Boolean
Dim sBackup As String = "RESTORE DATABASE BDBIBLIOTECA FROM DISK = '" & cadena & "'" & " WITH REPLACE"
Dim conecsb As New SqlConnectionStringBuilder
conecsb.DataSource = "(local)"
conecsb.InitialCatalog = "master"
conecsb.IntegratedSecurity = True
Using con As New SqlConnection(conecsb.ConnectionString)
Try
con.Open()
Dim cmdRestore As New SqlCommand(sBackup, con)
cmdRestore.ExecuteNonQuery()
restaurar_basededatos = True
Catch ex As Exception
restaurar_basededatos = False
MessageBox.Show(ex.Message)
Finally
con.Close()
End Try
End Using
End Function
Private Sub GUARDARToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GUARDARToolStripMenuItem.Click
Dim cadena As String
If (sfdguardar.ShowDialog() = Windows.Forms.DialogResult.OK) Then
cadena = sfdguardar.FileName
enlace.consultaBD("backup database BDBIBLIOTECA to disk = N'" + cadena + "'")
MessageBox.Show("Base de Datos Guardada", "Mensaje", MessageBoxButtons.OK)
End If
End Sub