From http://msdn.microsoft.com/library
The following walkthrough demonstrates the use of a custom action and
CustomactiondataProperty to create a database and database table during installation.
NoteThis walkthrough requires SQL server on the computer where you will deploy the application.
To create an installer class
- OnFileMenu, pointNew, And then chooseProject.
- InNew projectDialog box, selectVisual Basic ProjectsInProject TypePane, and then chooseClass LibraryInTemplatesPane. InNameBox, typeDbcustomaction.
- ClickOKTo close the dialog box.
- OnProjectMenu, chooseAdd new item.
- InAdd new itemDialog box, chooseInstaller class. InNameBox, typeDbcustomaction.
- ClickOKTo close the dialog box.
To create a data connection object
- In Server Explorer, selectData connections. Right-click and chooseAdd connection.
- InData Link PropertiesDialog box, do the following:
- Enter the server name.
- SelectUse Windows NT Integrated Security.
- In the database box, typeMaster.
- ClickOKTo close the dialog box.
- Drag the new connection and drop it onDbcustomaction. VBDesigner to createSqlconnection1 object.
To create a text file that contains a SQL statement to create a database
- In Solution Explorer, selectDbcustomactionProject. OnProjectMenu, chooseAdd new item.
- InAdd new itemDialog box, chooseText File. InNameBox, typeSQL .txt(Must be in lower case ).
- ClickOKTo close the dialog box.
- Add the following to the SQL .txt file:
CREATE TABLE [dbo].[Employees] ([Name] [char] (30) 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, selectSQL .txt. In the Properties window, setBuildactionPropertyEmbedded Resource.
To add code to the installer class to read the text file
- In Solution Explorer, selectDbcustomaction. VB. OnViewMenu, chooseCode.
- Add the followingImportsStatement at 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 ' Gets 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) ' Reads the contents of the embedded file. 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 that the connection ' is always closed. Command.Connection.Close() End TryEnd SubProtected Sub AddDBTable(ByVal strDBName As String) Try ' Creates the database. ExecuteSql("master", "CREATE DATABASE " + strDBName) ' Creates the tables. ExecuteSql(strDBName, GetSql("sql.txt")) Catch ex As Exception ' Reports any 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
- OnBuildMenu, chooseBuild dbcustomaction.
To create a deployment project
- OnFileMenu, chooseAdd Project,New project.
- InAdd ProjectDialog box, selectSetup and deployment projectsInProject TypePane, and then chooseSetup ProjectInTemplatesPane. InNameBox, typeDbcustomaction Installer.
- ClickOKTo close the dialog box.
- In the Properties window, selectProductnameProperty and typeDB Installer.
- In the file system Editor, selectApplication folder. OnActionMenu, chooseAdd,Project output.
- InAdd project output GroupDialog box, select the primary output forDbcustomactionProject. ClickOKTo close the dialog box.
To create a custom installation Dialog
- SelectDbcustomaction InstallerProject in Solution Explorer. OnViewMenu, pointEditor, And chooseUser Interface.
- In the User Interface Editor, selectStartNode underInstall. OnActionMenu, chooseAdd Dialog.
- InAdd DialogDialog box, selectTextboxes ()Dialog, then clickOKTo close the dialog box.
- OnActionMenu, chooseMove up. Repeat untilTextboxes ()Dialog is abveInstallation FolderNode.
- In the Properties window, selectBannertextProperty and typeSpecify Database Name.
- SelectBodytextProperty and typeThis dialog allows you to specify the name of the database to be created on the database server.
- SelectEdit1labelProperty and typeName of DB:.
- SelectEdit1propertyProperty and typeCustomtexta1.
- SelectEdit2visible,Edit3visible, AndEdit4visibleProperties and set themFalse.
To create a custom action
- SelectDbcustomaction InstallerProject in Solution Explorer. OnViewMenu, pointEditor, And chooseCustom Actions.
- In the Custom Actions Editor, selectInstallNode. OnActionMenu, chooseAdd Custom Action.
- InSelect item in ProjectDialog box, double-clickApplication folder.
- SelectPrimary output from dbcustomaction (active)Item, then clickOKTo close the dialog box.
- In the Properties window, selectCustomactiondataProperty and type/Dbname = [customtexta1].
- OnBuildMenu, chooseBuild dbcustomactioninstaller.
To install on your development computer
To deploy to another computer
- In Windows Explorer, navigate to your project directory and find the built Installer. The default path will be/Documents and Settings/Yourloginname/Dbcustomaction installer/Project Configuration/Dbcustomaction Installer. MSI. The defaultProject ConfigurationIs debug.
- Copy the dbcustomaction Installer. MSI file and all other files and subdirectories in the directory to another computer.
NoteTo install on a computer that is not on a network, copy the files to traditional media such as CD-ROM.
On the target computer, double-clickSetup.exeFile to run the installer.
NoteYou must have install permissions on the computer in order to run the installer.
To uninstall the application
- In the Windows Control Panel, double-clickAdd/Remove Programs.
- InAdd/Remove ProgramsDialog box, selectDbcustomaction InstallerAnd clickRemove, Then clickOKTo close the dialog box.
TipTo uninstall from your development computer, onProjectMenu, chooseUninstall.
See also
Custom Actions Management in deployment | customactiondata property | connecting to data sources with ADO. Net | error handling in custom actions