Software Technology Tips

We can check whether SQL express is installed on a machine or not and then can install it along with our application.Following is the code sample in VB.NET to implement the same.
[VB.NET CODE STARTS HERE]
 
' Need to add System.Management reference
Imports System.Management
 
 '   Check if SQL express is already installed in the destination machine
 
    Public Shared Function isExpressInstalled() As Boolean
        Const edition As String = "Express Edition"
        Const instance As String = "MSSQL$SQLEXPRESS"
        Const spLevel As Integer = 1
        Dim fCheckEdition As Boolean = False
        Dim fCheckSpLevel As Boolean = False
        Try
            Dim getSqlExpress As ManagementObjectSearcher = New ManagementObjectSearcher("root\Microsoft\SqlServer\ComputerManagement", "select * from    SqlServiceAdvancedProperty where SQLServiceType = 1 and ServiceName = '" + instance + "' and (PropertyName = 'SKUNAME' or PropertyName = 'SPLEVEL')")
            If getSqlExpress.Get.Count = 0 Then
                Return False
            End If
            For Each sqlEngine As ManagementObject In getSqlExpress.Get
                If sqlEngine("ServiceName").ToString.Equals(instance) Then
                    Select Case sqlEngine("PropertyName").ToString
                        Case "SKUNAME"
                            fCheckEdition = sqlEngine("PropertyStrValue").ToString.Contains(edition)
                        Case "SPLEVEL"
                            fCheckSpLevel = Integer.Parse(sqlEngine("PropertyNumValue").ToString) >= spLevel
                    End Select
                End If
            Next
            If fCheckEdition And fCheckSpLevel Then
                Return True
            End If
            Return False
        Catch e As ManagementException
            Return False
        End Try
    End Function
 
'   CODE FOR SQL INSTALLATION
 
        Dim msi As System.Diagnostics.Process = Nothing
        Dim blnExists As Boolean = isExpressInstalled()
 
        '   If not already installed then start SQL exe that is attached with the installer and placed where the application is present

        If Not blnExists Then
 
            Dim SqlAppPath As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
            msi = New System.Diagnostics.Process()
            msi.StartInfo.FileName = System.IO.Path.Combine(SqlAppPath, "SQLEXPR32.EXE")
            msi.Start()

        End If
 
[VB.NET CODE ENDS]


Related Tags:

VB.NET

Author: Amrita Dash

VB.NET

Let us Connect!

iso 9001 QA25 Red Herring STPI D&B Fastest Growing SME 2013 Award zinnov Nasscom

This site uses cookies. We respect your privacy.copyright (c) Mindfire Solutions 2007-2015. Login