1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
   | Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code qui sexécute au démarrage de lapplication
        'Je vérifie qu'il existe le service MSSQL$<nnomdel'instance>
        Dim Services() As ServiceController
        Services = ServiceController.GetServices()
        Dim instance() As String = Nothing
        For Each service As ServiceController In Services
            If service.ServiceName.Contains("MSSQL$") Then
                'S'il existe un service qui a dans son nom "MSSQL$", je récupère le nom de l'instance
                instance = Split(service.ServiceName, "$")
            End If
        Next
        If Not instance Is Nothing Then
            'si il existe le service, alors il y a un nom d'instance, alors j'attache la base de donnée
            Dim objSQL As SQLDMO.SQLServer
            'Sécurité avec le login windows
            objSQL.LoginSecure = True
            objSQL.Connect(My.Computer.Name + "\" + instance(1))
            For i As Int16 = 0 To objSQL.Databases.Count - 1
                If Not objSQL.Databases.Item(i).Name = "mabase" Then
                    'Je vérifie que la base de donnée n'est pas déjà dans le serveur, si elle n'y est pas je l'ajoute, en l'attachant.
                    objSQL.AttachDB("FrancisetJF", "[" + My.Request.PhysicalApplicationPath + "\App_Data\mabase.mdf],[" + My.Request.PhysicalApplicationPath + "\App_Data\mabase_log.ldf")
                End If
 
            Next
            objSQL.DisConnect()
        End If
    End Sub | 
Partager