.NET
WEB under the platform
Application deployment (
Install database and automatic configuration)
Li Honggen
Deploying a Web solution is more convenient under the .NET platform. We can use Visual Studio.net 2003 to add a web installation project, add the primary output and content files of the project in the deployed File System Editor, which is very simple to complete the production of the installer. However, the installer that is made is just the DLL file compiled by the web page and the ASP.NET program to the IIS directory of the target machine. For a general application, you can use the Access database, you can pack it into the installer together. If the database is SQL Server, you need to install the database when deploying, the installation program will be complex, and we need to customize the installer class. The SQL script is executed in the installer class and the connection string is written to Web.config. l Installing the Database Microsoft MSDN describes the establishment of a database when deploying an application. Such as: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/html/vxwlkwalkthroughusingcustomActiontOcreatedTabaseDuringInstallation.asp This method is to create a installer class, call in the installer class ADO.NET performs SQL statements (SQL statements in a text file) to create a database.
However, this approach has a problem. If you generate all the schedule, view, stored procedures with SQL Server2000, use ADO.NET to execute this script file, will have many "Go" statements in the script. An error occurred. Of course, we can replace "Go" into a newline, and use the ADO.NET article to execute the SQL statement. Obviously, such efficiency is relatively low.
The best way is to call OSQL execution scripts. (Or create a CMD file for a database project, and the CMD file is also called OSQL when the database is established.
First, we create a new ASP.NET web application, http: // localhost / vbnettest, and open the VBNetTest project
Create a deployment project
1. Point "Add Project" on the File menu and select New Project.
2. In the Add New Project dialog box, select Install and Deploy Projects in the Item Type pane, then select "Web Installation item" in the Template pane. Type Test Installer in the Name box.
3. Click "OK" to close the dialog.
4. The project is added to the Solution Explorer and the file system editor opens.
5. In the Properties window, select the productName property and type GCRM.
will
VBNettest
The output of the project is added to the deployment project
1. In the File System Editor, select the Web Application folder. On the "Operation" menu, point to Add, then select "Project Output".
2. In the Add Project Output Group dialog box, select "VBNetTest" in the Project drop-down list.
3. Click "OK" to close the dialog.
4. Select the "Main Output" and "Content File" group from the list, and then click OK.
Create an installer class
1. Point "New" on the File menu and select "Project".
2. In the New Project dialog box, select "Visual Basic Project" in the Project Type pane, then select "Class Library" in the Template pane. Type dbcustomAct in the Name box. 3. Click "Open" to close the dialog.
4. Select "Add New Item" from the Project menu.
5. Select "Install Program Class" in the Add New Item dialog. Type dbcustomAct in the Name box.
6. Click OK to close the dialog.
Create a custom installation dialog
1. Select the Test Installer project in the Solution Explorer. Point "Editor" on the View menu and select User Interface.
2. In the User Interface Editor, select the "Start" node under "Install". On the Actions menu, select Add Dialog.
3. In the Add Dialog dialog box, select the Law Protocol dialog box, and then click Close the dialog box.
4. In the Add Dialog Box dialog box, select the Text Box (a) dialog box and click "OK" to close the dialog.
5. On the "Operation" menu, select "Top". Repeat this step until the text box (a) dialog is on the "Install Folder" node.
6. In the Properties window, select the bannertext property and type: Install the database ..
7. Select the BodyText property and type: The installer will install the database on the target machine.
8. Select the Edit1Label property and type: Database Name:.
9. Select the Edit1Property property and type CustomTexta1.
10. Select the Edit1Value property and type: GSCRM.
11. Select Edit2Label attribute and type: Server Name:.
12. Select the Edit2Property property and type CustomTexta2.
13. Select Edit2Value properties and type: (local).
14. Select the Edit3Label property and type: Username:.
15. Select the Edit3Value property and type: sa.
16. Select the Edit3Property property and type CustomTexta3.
17. Select the Edit4Label property and type: password:.
18. Select the Edit4Property property and type CustomTexta4.
19. Select Edit2Visible, Edit3Visible, and Edit4Visible properties and set them to false.
Create custom operations
1. Select the Test Installer project in the Solution Explorer. Point "Editor" on the View menu and select Customize.
2. Select the "Install" node in the Custom Operation Editor. On the Actions menu, select Add Custom Action.
3. In the "Select Item" dialog box, double-click Application Folder.
4. Select the "Main Output" item from the DBCustomAction, and then click "OK" to close the dialog.
5. In the Properties window, select the CustomActionData property and type / dbname = [CustomTexta1] / Server = [CustomTexta2] / user = [CustomTexta3] / PWD = [CustomTextA4] / Targetdir = "[targetdir] /". Attached / targetdir = "[targetdir] /" is the installed target path, in order to get the installed path in the DBCustomAction class, we set this parameter.
In addition, the path after the installation can also be obtained by reflection:
DIM ASM as system.reflection.assembly = _
System.Reflection.Assembly.GetexecutingAsembly
Msgbox ("asm.location")
add files
1. Add the script file DB.SQL generated by SQL Server to the Test Installer project
2. Add the installation file LisenseFile.rtf to the Test Installer project
3. In the User Interface Editor, select the license agreement, set the LisenseFile property to the Lisncefile.rtf file.
Document in the project
:
Add code to the installer class, dbcustomaction.vb class
Imports system.componentmodel
Imports system.configuration.install
Imports system.io
Imports system.reflection
Inherits System.Configuration.install.installer
#REGION "Component Designer Generated Code"
Public Sub New ()
Mybase.new ()
'This call is necessary for the component designer
InitializeComponent ()
'Add any initialization after INITIALIZECOMPONENT ()
End Sub
'Installer Overwrites Dispose to clean the component list.
Protected Overloads Overrides Sub Dispose (Byval Disposing as Boolean)
IF Disposing then
IF not (Components Is Nothing) THEN
Components.dispose ()
END IF
END IF
Mybase.dispose (Disposing)
End Sub
Private Components as System.comPonentModel.icontainer
End Sub
#End region
'Execute SQL statement
Private Sub Executesql (Byval CONN As String, Byval DatabaseName As String, Byval SQL AS String)
DIM mysqlconnection as new sqlclient.sqlConnection (conn)
Dim command as new sqlclient.sqlcommand (SQL, MySQLConnection)
Command.connection.open ()
Command.connection.changedatabase (DatabaseName) TRY
Command.executenonQuery ()
Finally
'Close Connection
Command.connection.close ()
END TRY
End Sub
Public overrides sub install (byval statesaver as system.collections.idictionary)
Mybase.install (StateSaver)
' ------------------------build database----------------------- ----------------------------
Try
DIM Connstr as string = string.format ("data source = {0}; user ID = {1}; password = {2}; Persist security info = false; packet size = 4096, me.context.parameters.Item ( "server"), me.context.parameters.Item ("User"), me.context.parameters.Item ("PWD"))
'Set up the database based on the input database name
ExecuteSQL (Connstr, "Master", "Create Database" Me.Context.Parameters.Item ("DBNAME"))
'Call OSQL execution script
Dim SqlProcess As New System.diagnostics.Process
SqlProcess.StartInfo.FileName = "Osql.exe"
SqlProcess.startinfo.rguments = string.format ("-u {0} -p {1} -d {2} -i {3} db.sql", me.context.Parameters.Item ("user"), ME .Context.Parameters.Item ("PWD"), Me.Context.Parameters.Item ("DBNAME"), Me.Context.Parameters.Item ("Targetdir"))
SqlProcess.startinfo.windowstyle = ProcessWindowStyle.hidden
SqlProcess.Start ()
SQLPROCESS.WAITFOREXIT () 'Waiting for execution
SQLPROCESS.CLOSE ()
'Delete script files
Dim sqlfileinfo as new system.io.fileinfo (String.Format ("{0} db.sql", me.context.parameters.Item ("targetdir"))))))))
If SQLFILEINFO.EXISTS THEN
SQLFILEINFO.DELETE ()
END IF
Catch exception
Throw EX
END TRY
'--------------------- Write the connection string to Web.config ------------------- ----------------
Try
Dim fileinfo as system.io.fileinfo = new system.io.fileinfo (me.context.parameters.Item ("targetdir") & "/web.config")if not fileinfo.exists the
Throw new installexception ("No configuration file found")
END IF
'Instantiate XML documentation
Dim xmldocument as new system.xml.xmldocument
XmlDocument.Load (fileinfo.fullname)
'Find nodes in appsettings
Dim node as system.xml.xmlnode
Dim foundit as boolean = false
For Each Node in XmlDocument.Item ("configuration"). Item ("appsettings")
If node.name = "add" then
If node.attributes.getnamedItem ("key"). Value = "connString" then
'Write connection string
Node.attributes.getnamedItem ("Value"). Value = string.format ("Persist security info = false; data source = {0}; initial catalog = {1}; user ID = {2}; password = {3} Packet size = 4096; pooling = true; max pool size = 100; min pool size = 1 ", _
Me.Context.Parameters.Item ("Server"), Me.Context.Parameters.Item ("DBNAME"), Me.Context.Parameters.Item ("User"), Me.Context.Parameters.Item ("PWD" ))
Foundit = TRUE
END IF
END IF
Next node
IF notfactim
Throw new installexception ("Web.config file does not contain connString connection string settings"))
END IF
XMLDocument.save (fileinfo.fullname)
Catch exception
Throw EX
END TRY
End Sub
END CLASS
Finally compile generation!
Installation interface:
Disclaimer: The right to copyright and interpretation of this article belongs to Li Honggen, if you need to reprint, please keep your full content and this statement.
QQ: 21177563 MSN:
Lihonggen@hotmail.com
Column: http://www.9cbs.net/develop/author/netauthor/lihonggen0/0/