Listing 1	VB.NET
Create the Database. After retrieving server name, username, and password information for the SQL Server database through a dialog box, this code creates the InvoiceDB database, filling it with records.

Imports Microsoft.Win32
Imports System.Data.SqlClient
Imports System.IO
Imports System.Reflection

Public Class frmSQLServer
	Inherits System.Windows.Forms.Form

	Public strConnection As String

	Private Sub btnOK_Click(ByVal sender As _
		System.Object, ByVal e As _
		System.EventArgs) _
		Handles btnOK.Click
			' Checking for data presence
			If txtServerName.Text.Length = 0 Then
				MsgBox("Please, insert the " & _
						"server name")
				Exit Sub
			End If

			If txtUsername.Text.Length = 0 Then
				MsgBox("Please, insert the" & _
						" username")
				Exit Sub
			End If

			' Build connection string to connect 
			' to the master database
			strConnection = "server=" & _
							txtServerName.Text & _
						";database=master;uid=" & _
							txtUsername.Text & _
							";pwd=" & txtPassword.Text

			' Call this method to 
			' create the database
			CreateDB("InvoiceDB")

			Try
				' Open the registry looking for 
				' application keys
				Dim key As RegistryKey = _
			Registry.LocalMachine.OpenSubKey(_
							"SOFTWARE\Fawcette" & _
							"\InvoiceManager", & _
							True)

				If Not key Is Nothing Then
					' I found them so I can write new 
					' values. Note I replace 
					' master string 
					' with database name string.
						key.SetValue(_
							"DBConnectionString", _
							strConnection.Replace(_
							"master", _
							"InvoiceDB"))
					key.SetValue("IsSQLServer", 1)
					key.Close()
				End If

			Catch ex As Exception
				MsgBox("Install: " & ex.Message)
				Throw ex
			Finally
				Me.Close()
			End Try

	End Sub

	Public Sub ExecuteSql(ByVal DatabaseName As String, _
		ByVal Sql As String)
		' Create objects to connect to the database
		' and run a SQL Command
		Dim dbConn As New _
			SqlClient.SqlConnection(strConnection)
		Dim Command As New _
			SqlClient.SqlCommand(Sql, dbConn)

		Try
			' Open the database connection
			Command.Connection.Open()

			' Change the database pointing to the
			' specified ones
			Command.Connection.ChangeDatabase(_
							DatabaseName)

            ' Execute the provided SQL statement
            Command.ExecuteNonQuery()
		Catch ex As Exception
			MsgBox("ExecuteSQL: " & ex.Message)
			Throw ex
			Me.Close()
		Finally
			If Command.Connection.State = _
				ConnectionState.Open Then
				Command.Connection.Close()
			End If
		End Try

	End Sub

	Private Function ReadSql_
				(ByVal Name As String) As String
		Try
			' Get the current assembly.
			Dim Asm As [Assembly] = _
				[Assembly].GetExecutingAssembly()

			' Resources are named using a fully 
			' qualified	name.
			Dim strName As String
			strName = Asm.GetName().Name + _
							"." + Name
			Dim strm As Stream = _
				Asm.GetManifestResourceStream(_
							strName)

			' Read the contents of the 
			' embedded file.
			Dim reader As StreamReader = New _
				StreamReader(strm)
			Return reader.ReadToEnd()

		Catch ex As Exception
			MsgBox("ReadSQL: " & ex.Message)
			Throw ex
			Me.Close()
		End Try

	End Function

	Protected Sub CreateDB _
						(ByVal strDBName As String)
		Try
			' Create the database.
			ExecuteSql("master", _
					"CREATE DATABASE " + _
						strDBName)

			' Create the table.
			ExecuteSql(strDBName, _
					ReadSql("InvoiceDB.sql"))

			' Create stored procedures
			ExecuteSql(strDBName, _
				ReadSql("spSearchByDate.sql"))
			ExecuteSql(strDBName, _
				ReadSql("spSearchBy" & _
						"InvoiceNumber.sql"))
			ExecuteSql(strDBName, _
				ReadSql("spSearchByName.sql"))

		Catch ex As Exception
			' Report any errors and abort.
			MsgBox("In exception handler: " & _
							ex.Message)
			Throw ex
			Me.Close()
		End Try
	End Sub

End Class



Listing 2	VB.NET
Store User-Provided Information in the Registry. The Uninstall() method is called during application uninstall. The code in the Uninstall() method can't access Windows Installer information the user provides during the application installation, so you should store that information in the Windows Registry.

Public Overrides Sub Uninstall(ByVal savedState As _
	IDictionary)
	' First of all we have to call
	' the base method
	MyBase.Uninstall(savedState)

	' Read information from the registry
	Dim key As RegistryKey = _
		Registry.LocalMachine.OpenSubKey("S" & _
			"OFTWARE\Fawcette\InvoiceManager", _
							True)
	Dim strConnection As String
	Dim iIsSqlServer As Integer = 0
	If Not key Is Nothing Then
		strConnection = _
		key.GetValue("DBConnectionString")
		iIsSqlServer = key.GetValue("IsSQLServer")
		key.Close()
	End If

	' If user choice was MSSQL then
	' execute the SQL drop statement
	If iIsSqlServer = 1 Then
		Dim myForm As New frmSQLServer()
		myForm.strConnection = strConnection
		myForm.ExecuteSql("master", _
			"DROP DATABASE InvoiceDB")
	End If
End Sub


