(Visual Studio 2008) Fermer correctement une base de données MDF

Traitée (Visual Studio 2008) Fermer correctement une base de données MDF

  • vendredi 23 novembre 2012 17:22
     
      A du code

    Bonjour,

    Voici le code d'ouverture de la base

        Public MaBase As String = My.Application.Info.DirectoryPath & "\Fic\" & NomBase
        Dim strcon As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=" & MaBase & ";Integrated Security=True;User Instance=True"
        Public wNbrENRG As Integer  '   Nombre enregistrements ou ligne trouvé
        Public wIndERR As Integer   '   Indice pour faire une boucle sur type des erreurs
        Public sqlRequete As String '   Pour les SELECT
        Public SqlDR As SqlDataReader
        Public SqlConx As New SqlConnection(strcon)
        'Public SqlConx As New SqlConnection(My.Settings.commandeConnectionString)   '   Paramètre de l'aplication
        Public SqlCmd As SqlCommand
        Public wDataSet As New DataSet
        Public wSqlDatAda As SqlDataAdapter
        Public Sqltrans As SqlTransaction
        Public Sub ConnectionBase()
            Try
                If SqlConx.State = ConnectionState.Closed Then
                    SqlConx.Open()
                End If
            Catch ex As SqlException
                For wModBaseDonne.wIndERR = 0 To ex.Errors.Count - 1
                    MsgErreur = "ConnectionBase - Index #" & wModBaseDonne.wIndERR.ToString() & ControlChars.NewLine _
                        & "Message: " & ex.Errors(wModBaseDonne.wIndERR).Message & ControlChars.NewLine _
                        & "LineNumber: " & ex.Errors(wModBaseDonne.wIndERR).LineNumber & ControlChars.NewLine _
                        & "Source: " & ex.Errors(wModBaseDonne.wIndERR).Source & ControlChars.NewLine _
                        & "Procedure: " & ex.Errors(wModBaseDonne.wIndERR).Procedure & ControlChars.NewLine
                    MessageBox.Show(MsgErreur)
                Next wModBaseDonne.wIndERR
                wOk = False
            Catch ex As Exception
                MsgBox("ConnectionBase " & ex.Message)
                wOk = False
            End Try
        End Sub

    Et son code fermeture

        Public Sub Fermeture()
            Try
                If Not SqlDR Is Nothing Then
                    SqlDR.Close()
                    SqlDR = Nothing
                End If
                If Not SqlCmd Is Nothing Then
                    'SqlCmd.Dispose()
                    SqlCmd = Nothing
                End If
                If Not wDataSet Is Nothing Then
                    'wDataSet.Dispose()
                    wDataSet = Nothing
                End If
                If Not wSqlDatAda Is Nothing Then
                    'wSqlDatAda.Dispose()
                    wSqlDatAda = Nothing
                End If
    
                If Not SqlConx Is Nothing Then
                    If SqlConx.State = ConnectionState.Open Then
                        SqlConx.Close() 'possibilité de vérifier l'état de connexion pour rendre + robuste
                        'SqlConx.Dispose()
                        'SqlConx = Nothing
                    End If
                End If
    
                SqlConnection.ClearPool(SqlConx)
    
                GC.Collect()
    
            Catch ex As SqlException
                For wModBaseDonne.wIndERR = 0 To ex.Errors.Count - 1
                    MsgErreur = "Fermeture - Index #" & wModBaseDonne.wIndERR.ToString() & ControlChars.NewLine _
                        & "Message: " & ex.Errors(wModBaseDonne.wIndERR).Message & ControlChars.NewLine _
                        & "LineNumber: " & ex.Errors(wModBaseDonne.wIndERR).LineNumber & ControlChars.NewLine _
                        & "Source: " & ex.Errors(wModBaseDonne.wIndERR).Source & ControlChars.NewLine _
                        & "Procedure: " & ex.Errors(wModBaseDonne.wIndERR).Procedure & ControlChars.NewLine
                    MessageBox.Show(MsgErreur)
                Next wModBaseDonne.wIndERR
                wOk = False
            Catch ex As Exception
                MsgBox("Fermeture " & ex.Message)
                wOk = False
            End Try
        End Sub

    Je lance la fermeture et veux copier la base

    J'ai l'erreur

    Comment faire pour fermer correctement ?


    Cordialement Sauveur CONSALVI

Toutes les réponses