USING a Custom Action To Create A Database DURING Installation

xiaoxiao2021-03-06  81

From http://msdn.microsoft.com/library

The Following Walkthrough DemonStrates The Use of a Custom Action and The

CustomActionData Property to Create A Database and Database Table During Installation.

Note this Walkthrough Requires SQL Server On The Computer Where You Will Deploy The Application.

To create an installer class

On the File menu, point to New, and then choose Project. In the New Project dialog box, select Visual Basic Projects in the Project Type pane, and then choose Class Library in the Templates pane. In the Name box, type DBCustomAction. Click ON The Project Menu, Choose Add New Item. In The Add New Item Dialog Box, Choose Installer Class. In The Name Box, Type DbcustomAction. Click Ok To Close The Dialog Box.

TO CREATE A DATA Connection Object

In Server Explorer, Select Data Connections. Right-Click and Choose Add Connection. In The Data Link Properties Dialog Box, Do The Following:

Enter the server name. Select Use Windows NT Integrated Security. In the database box, type master. Click OK to close the dialog box. Drag the new connection and drop it on the DBCustomAction.vb designer to create a sqlConnection1 object.

To create a Text File That Contains a SQL Statement To create a database

In Solution Explorer, select the DBCustomAction project. On the Project menu, choose Add New Item. In the Add New Item dialog box, choose Text File. In the Name box, type sql.txt (must be in lower case). Click OK To Close The Dialog Box. Add The Following To The Sql.txt File: Create Table [DBO]. [Employees].

[Name] [char] (30) Collate SQL_LATIN1_GENER_CP1_CI_AS NULL,

[Rsvp] [int] NULL,

[Requests] [nvarchar] (4000) Collate SQL_LATIN1_GENER_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 property window, set the buildaction property to embedded resource.

To add code to the installer class to read the text file

In Solution Explorer, Select DbcustomAction.vb. On The View Menu, Choose Code. Add The Following Imports Statement At The Top of The Module: Imports System.io

Imports 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 exception

Msgbox ("in getsql:" & ex.Message)

Throw EX

END TRY

END FUNCTION

Private 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 TRY

End Sub

Protected Sub AddddbTable (byval strdbname as string)

Try

'Creates the Database.

ExecuteSQL ("Master", "Create Database" StrdbName)

'Creates the Tables.

ExecuteSQL (strDbname, getsql ("sql.txt")))

Catch exception

'Reports Any Errors and Abort.msgBox ("in Exception Handler:" & ex.Message)

Throw EX

END TRY

End Sub

Public overrides sub install (byval statesaver as system.collections.idictionary)

Mybase.install (StateSaver)

Adddbtable (Me.Context.Parameters.Item ("DBNAME"))

End Sub on the build menu, choield dbcustomaction.

To create a Deployment Project

On the File menu, choose Add Project, New Project. In the Add Project dialog box, select Setup and Deployment Projects in the Project Type pane, and then choose Setup Project in the Templates pane. In the Name box, type DBCustomAction Installer. Click OK to close the dialog box. In the Properties window, select the ProductName property and type DB Installer. In the File System Editor, select the Application Folder. On the Action menu, choose Add, Project Output. In the Add Project Output Group dialog Box, Select The Primary Output for the dbcustomaction project. Click Ok to Close The Dialog Box.

To create a Custom Installation Dialog

Select the DBCustomAction Installer project in Solution Explorer. On the View menu, point to Editor, and choose User Interface. In the User Interface Editor, select the Start node under Install. On the Action menu, choose Add Dialog. In the Add Dialog dialog box, select the Textboxes (A) dialog, then click OK to close the dialog box. On the Action menu, choose Move Up. Repeat until the Textboxes (A) dialog is above the Installation Folder node. In the Properties window, select the BannerText property and type Specify Database name. Select the BodyText property and type This dialog allows you to specify the name of the database to be created on the database server. 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 The False.to Create a Custom Action

Select the DBCustomAction Installer project in Solution Explorer. On the View menu, point to Editor, and choose Custom Actions. In the Custom Actions Editor, select the Install node. On the Action menu, choose Add Custom Action. In the Select item in project dialog box, double-click the Application Folder. Select the Primary output from DBCustomAction (Active) item, then click OK to close the dialog box. In the Properties window, select the CustomActionData property and type / dbname = [CUSTOMTEXTA1]. On the Build Menu, Choose Build DbcustomactionInstaller.

To Install ON Your Development Cubputer

..................................

Note You Must Have Install Permissions on The Computer in Order to Run The Installer.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 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 On A NetWork, Copy The Files To Traditional Media Such AS CD-ROM.

On The Target Computer, Double-Click The Setup.exe File To Run The Installer.

Note You Must Have Install Permissions on The Computer in Order To Run The Installer.

To uninstall the application

In The Windows Control Panel, Double-Click Add / Remove Program. In The Add / Remove Program Dialog Box, Select Dbcustomaction Installer And Click Remove, The Click Ok to Close The Dialog Box.

Tip to uninstall from your development computer, on the

Project Menu, Choose

Uninstall.

See Also

Custom ActionS Management In Deployment | CustomActionData Property | Connecting to Data Sources with ADO.NET | ERROR HANDLING IN CUSTOM ACTIONS

转载请注明原文地址:https://www.9cbs.com/read-108246.html

New Post(0)