Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.IO
Imports System.Data.SqlClient
Imports System.Security
Imports System.Security.Cryptography
Imports System.Xml

Public Class InstallerSource

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()

        'Add initialization code after the call to InitializeComponent

    End Sub

    Public Shared Function CheckDatabaseExists(ByVal Server As String, ByVal Database As String) As Boolean
        Dim connString As String = ("Data Source=" _
                    + (Server + ";Initial Catalog=Master;Integrated Security=True;"))
        Dim cmdText As String = ("SELECT * FROM master.dbo.sysdatabases WHERE NAME=\'" _
                    + (Database + "\'"))
        Dim bRet As Boolean = False
        Using sqlConnection As SqlConnection = New SqlConnection(connString)
            sqlConnection.Open()
            Using sqlCmd As SqlCommand = New SqlCommand(cmdText, sqlConnection)
                Using reader As SqlDataReader = sqlCmd.ExecuteReader
                    bRet = reader.HasRows
                End Using
            End Using
        End Using
        Return bRet
    End Function

    Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
        Try
            MyBase.Install(stateSaver)

            Dim sqlExpress As String = "\SQLEXPRESS"
            Dim machineName As String = "."
            Dim dataSource As String = machineName & sqlExpress
            Dim connString As String = String.Format("Persist Security Info=False;Integrated Security=SSPI;Initial Catalog=master;Data Source={0}", dataSource)

            'Save the database connection string to install and uninstall later
            stateSaver.Item("installDatabaseConnection") = connString


            'Create the database first
            ExecuteSql(My.Resources.ResourceFiles.DbScriptCreate.Replace("GO", ""), stateSaver.Item("installDatabaseConnection"))
            'Create the tables next
            ExecuteSql(My.Resources.ResourceFiles.DbScriptCreateTables.Replace("GO", ""), stateSaver.Item("installDatabaseConnection"))
            'System.Diagnostics.Debugger.Break()


        Catch ex As NullReferenceException
            Throw New InstallException("Error in Install " & ex.Message)
        Catch ex As ApplicationException
            Throw New InstallException("Error in Install " & ex.Message)
        Catch ex As Exception
            Throw New InstallException("Error in Install " & ex.Message)

        End Try
    End Sub


    Private Sub ExecuteSql(ByVal SqlComm As String, ByVal connection As String)

        Dim sqlConn As New SqlConnection(connection)

        Try
            'Initialize the connection
            Using sqlCmd As New SqlCommand(SqlComm, sqlConn)

                If sqlCmd.Connection.State = ConnectionState.Closed Then
                    sqlCmd.Connection.Open()
                End If

                'Execute the sql
                sqlCmd.ExecuteNonQuery()

            End Using

        Catch ex As NullReferenceException
            Throw ex
        Catch ex As ApplicationException
            Throw ex
        Catch ex As Exception
            Throw ex

        End Try
    End Sub

    Public Overrides Sub Uninstall(ByVal savedState As System.Collections.IDictionary)
        Try

            MyBase.Uninstall(savedState)

            'Drop the database
            If savedState.Contains("installDatabaseConnection") Then

                ExecuteSql(My.Resources.ResourceFiles.DbScriptDrop.Replace("GO", ""), savedState("installDatabaseConnection"))
            End If

        Catch ex As NullReferenceException
            Throw New InstallException("Error in Install " & ex.Message)
        Catch ex As ApplicationException
            Throw New InstallException("Error in Install " & ex.Message)
        Catch ex As Exception
            Throw New InstallException("Error in Install " & ex.Message)
        End Try
    End Sub

    Public Overrides Sub Rollback(ByVal savedState As System.Collections.IDictionary)
        Try
            MyBase.Rollback(savedState)

            If savedState.Contains("installDatabaseConnection") Then
                'Drop the database
                ExecuteSql(My.Resources.ResourceFiles.DbScriptDrop.Replace("GO", ""), savedState("installDatabaseConnection"))
            End If

        Catch ex As NullReferenceException
            Throw New InstallException("Error in Install " & ex.Message)
        Catch ex As ApplicationException
            Throw New InstallException("Error in Install " & ex.Message)
        Catch ex As Exception
            Throw New InstallException("Error in Install " & ex.Message)
        End Try
    End Sub

   
End Class
