The following walkthrough demonstrates how to use a custom action during installation and
CustomActionDataproperty to create a database and database table.
To Create an installer class
- On the File menu, point to New, and then select Project.
- In the New Project dialog box, select Visual Basic project in the Project Types pane, and then select Class Library in the Templates pane. In the Name box, type dbcustomaction.
- From the Project menu, choose Add New Item.
- In the Add New Item dialog box, select Setup class. In the Name box, type dbcustomaction.
To create a data connection object
- In Server Explorer, select Data Connections. Right-click and select Add Connection.
- In the Data Link Properties dialog box, enter a valid server name, user name, and password value for your SQL Server configuration. In the Database box, type master. Select the Allow saving password check box.
- Drag the new connection to the DBCustomAction.vb designer to create the SqlConnection1 object.
Create a text file that contains SQL statements to create a database
- In Solution Explorer, select the DBCustomAction project. From the Project menu, choose Add New Item.
- In the Add New Item dialog box, select Text file. In the Name box, type sql.txt(must be lowercase).
- Add the following content to the Sql.txt file:
CREATE TABLE [dbo]. [Employees] ([Name] [char] COLLATE sql_latin1_general_cp1_ci_as NOT NULL, [RSVP] [int] NULL, [Requests] [nvarchar] (4000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL) on [PRIMARY]; ALTER TABLE [dbo]. [Employees] With NOCHECK ADD CONSTRAINT [pk_employees] PRIMARY KEY CLUSTERED ([Name]) on [PRIMARY];
- In Solution Explorer, select Sql.txt. In the Properties window, set the Build Action property to embedded resources.
Add code to the Installer class to read a text file
- In Solution Explorer, select DBCustomAction.vb. On the View menu, select Code.
- Add the following Imports statement to the top of the module:
Imports System.ioimports System.Reflection
- Add the following code to the class:
Private Function GetSQL (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 strm as Stream = Asm.getmanifestresourcestream (Asm.getname (). Name + "." + name) ' Read the contents of the embedded FILE.    &N Bsp; dim reader as StreamReader = New StreamReader (strm) return reader. ReadToEnd () catch ex as Exception msgbox ("in GetSQL:" & Ex. Message) throw ex end tryend functionprivate Sub ExecuteSql ( ByVal DatabaseName As String, ByVal SQL as String) dim Command as New sqlclient.sqlcommand (SQL, sqlconnEction1) command.connection.open () command.connection.changedatabase ( DatabaseName) try command.executenonquery () Finally ' Finally, blocks are a great way to ensure the CONNECTION &NB sp; ' is always closed. command.connection.close ( ) end tryend subprotected Sub adddbtable (ByVal strdbname as String) Try ' Create ' Database. executesql ("Master", "Create DATABASE "+ strdbname" ' Create the tables. ExecuteSQL (strDbName, GetSQL ("Sql.txt")) catch ex as exception ' Errors and Abort. msgbox ("in exception Handler: "& Ex." Message) throw ex end tryend SubPublic Overrides Sub Install (ByVal statesaver as System.Collections.IDictionary) mybase.install (statesaver) adddbtable (Me.Context.Parameters.Item ("dbname")) End Sub
- On the Build menu, select Generate DBCustomAction.
To Create a deployment project
- On the File menu, select Add Project-> new project.
- In the Add Project dialog box, select Install and deploy projects in the Project Types pane, and then select Setup Project in the Templates pane. In the Name box, type dbcustomaction Installer.
- In the Properties window, select the ProductName property and type the DB Installer.
- In the File System Editor, select Application Folder. On the Action menu, select Add-> project output.
- In the Add Project Output Group dialog box, select the primary output for the DBCustomAction project.
To Create a Custom installation dialog box
- Select the DBCustomAction Installer project in Solution Explorer. On the View menu, point to Editor, and then select User interface.
- In the User Interface Editor, select the startup node under install. On the Action menu, select Add dialog box.
- In the Add dialog box, select the text Box (A) dialog box.
- On the Action menu, select Move Up. Repeat this step until the text box (A) dialog box is above the installation folders node.
- In the Properties window, select the bannertext property and type specify Database Name.
- Select the BodyText property and type This dialog allows your to specify the name of the "database to" created on the database SE RVer.
- Select the Edit1Label property and type Name of DB:.
- Select the edit1property property and type CUSTOMTEXTA1.
- Select the edit2visible,edit3visible , and edit4visible properties and set them to False.
Create a custom action
- Select the DBCustomAction Installer project in Solution Explorer. On the View menu, point to Editor, and then select Custom Actions.
- Select the Install node in the Custom Action Editor. On the Action menu, select Add Custom action.
- In the Select Item in Project dialog box, double-click Application Folder.
- Select the primary output from DBCustomAction (Active) item.
- In the Properties window, select the customactiondata property and type /dbname=[customtexta1].
- On the Build menu, choose Generate Dbcustomactioninstaller.
Installing on the development computer
- Select the DBCustomAction Installer project in Solution Explorer. On the Project menu, choose Install.
This will run the Setup program on the development computer.
deploy to another computer
- In Windows Explorer, navigate to the project directory and look for the generated installer. The default path will be \documents and Settings\yourloginname\dbcustomaction installer\project configuration\ DBCustomAction Installer.msi. The default "project configuration" is "Debug."
- Copy the DBCustomAction Installer.msi file and all other files and subdirectories in the directory to another computer.
Note to install on a computer that is not connected to the network, copy the file to a traditional media such as a CD-ROM.
Double-click the Setup.exe file on the target computer to run Setup.
Uninstalling Applications
- In Windows Control Panel, double-click Add/Remove Programs.
- In the Add/Remove Programs dialog box, select DBCustomAction Setup and click Remove.
tip To uninstall from the development computer, on the Project menu, select Uninstall.
Please see
Custom action Management in Deployment | CustomActionData Properties | Connecting to a data source via Ado.net | Error handling in Custom actions