關於工程帶資料庫打包(1) (轉)
有不少的朋友都遇到過這樣一個問題:工程如何代一起打包。下面的MSDN文件對這個問題作了簡單的說明:
.com/library/default.?url=/library/en-us/vsintro7/html/vxwlkWalkthroughUsingCustomActionToCreateDatabaseDuringInstallation.asp">URL::namespace prefix = o ns = "urn:schemas-microsoft-com::office" />
全文如下:copy from MSDN。
《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《
Using a Custom Action to Create a Database During Installation:
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 SERVER on the computer where you will deploy the application.
To create an installer class
1. On the File menu, point to New, and then choose Project.
2. In the New Project dialog box, Projects in the Project Type pane, and then choose Class Library in the Templates pane. In the Name box, type CustomAction.
3. Click OK to close the dialog box.
4. On the Project menu, choose Add New Item.
5. In the Add New Item dialog box, choose Installer Class. In the Name box, type DBCustomAction.
6. Click OK to close the dialog box.
To create a data connection
1. In Server Explorer, select Data Connections. Right-click and choose Add Connection.
2. In the Data Link Properties dialog box, do the following:
a. Enter the server name.
b. Select Use NT Integrated Security.
c. In the database box, type master.
d. Click OK to close the dialog box.
3. 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
1. In Solution Explorer, select the DBCustomAction project. On the Project menu, choose Add New Item.
2. In the Add New Item dialog box, choose Text File. In the Name box, type sql.txt (must be in lower case).
3. Click OK to close the dialog box.
4. Add the following to the sql.txt file:
5. CREATE TABLE [dbo].[Employees] (
6. [Name] [char] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
7. [Rsvp] [int] NULL ,
8. [Requests] [nvarchar] (4000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
9. ) ON [PRIMARY];
10.
11. ALTER TABLE [dbo].[Employees] WITH NOCHECK ADD
12. CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED
13. (
14. [Name]
) ON [PRIMARY];
15. In Solution Explorer, select sql.txt. In the Properties window, set the BuildAction property to Embedded Re.
To add code to the installer class to read the text file
1. In Solution Explorer, select DBCustomAction.vb. On the View menu, choose Code.
2. Add the following Imports statement at the top of the module:
3. Imports System.IO
Imports System.Reflection
4. Add the following code to the class:
5. Private Function GetSql(ByVal Name As String) As String
6. Try
7.
8. ' Gets the current assembly.
9. Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()
10.
11. ' Resources are named using a fully qualified name.
12. Dim stAs Stream = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name)
13.
14. ' Reads the contents of the embedded file.
15. Dim reader As StreamReader = New StreamReader(strm)
16. Return reader.ReadToEnd()
17. Catch ex As Exception
18. MsgBox("In GetSQL: " & ex.Message)
19. Throw ex
20. End Try
21.
22. End Function
23.
24. Private Sub ExecuteSql(ByVal DatabaseName As String, ByVal Sql As String)
25. Dim Command As New SqlClient.SqlCommand(Sql, sqlConnection1)
26.
27. Command.Connection.Open()
28. Command.Connection.ChangeDatabase(DatabaseName)
29. Try
30. Command.ExecuteNonQuery()
31. Finally
32. ' Finally, blocks are a great way to ensure that the connection
33. ' is always closed.
34. Command.Connection.Close()
35. End Try
36. End Sub
37.
38. Protected Sub AddDBTable(ByVal strDBName As String)
39. Try
40. ' Creates the database.
41. ExecuteSql("master", "CREATE DATABASE " + strDBName)
42.
43. ' Creates the tables.
44. ExecuteSql(strDBName, GetSql("sql.txt"))
45.
46. Catch ex As Exception
47. ' Reports any errors and abort.
48. MsgBox("In exception handler: " & ex.Message)
49. Throw ex
50. End Try
51. End Sub
52.
53. Public Overrs Sub Install(ByVal stateSaver As System.Collections.IDictionary)
54. MyBase.Install(stateSaver)
55. AddDBTable(Me.Context.Parameters.Item("dbname"))
End Sub
56. On the Build menu, choose Build DBCustomAction.
To create a deployment project
1. On the File menu, choose Add Project, New Project.
2. 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.
3. Click OK to close the dialog box.
4. In the Properties window, select the ProductName property and type DB Installer.
5. In the File System Editor, select the Application Folder. On the Action menu, choose Add, Project Output.
6. 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
1. Select the DBCustomAction Installer project in Solution Explorer. On the View menu, point to Editor, and choose User Interface.
2. In the User Interface Editor, select the Start node under Install. On the Action menu, choose Add Dialog.
3. In the Add Dialog dialog box, select the Textboxes (A) dialog, then click OK to close the dialog box.
4. On the Action menu, choose Move Up. Repeat until the Textboxes (A) dialog is above the Installation Folder node.
5. In the Properties window, select the BannerText property and type Specify Database Name.
6. Select the BodyText property and type This dialog allows you to specify the name of the database to be created on the database server.
7. Select the Edit1Label property and type Name of DB:.
8. Select the Edit1Property property and type CUSTOMTEXTA1.
9. Select the Edit2Visible, Edit3Visible, and Edit4Visible properties and set them to false.
To create a custom action
1. Select the DBCustomAction Installer project in Solution Explorer. On the View menu, point to Editor, and choose Custom Actions.
2. In the Custom Actions Editor, select the Install node. On the Action menu, choose Add Custom Action.
3. In the Select item in project dialog box, double-click the Application Folder.
4. Select the Primary output from DBCustomAction(Active) item, then click OK to close the dialog box.
5. In the Properties window, select the CustomActionData property and type /dbname=[CUSTOMTEXTA1].
6. On the Build menu, choose Build DBCustomActionInstaller.
To install on your development computer
- Select the DBCustomAction Installer project in Solution Explorer. On the Project menu, choose Install.
This will run the installer on your development computer.
Note You must have install pessions on the computer in order to run the installer.
To deploy to another computer
1. In Windows Explorer, navigate to your project directory and find the built installer. The default path will be documents and settingsyourloginnameDBCustomAction Installerproject configurationDBCustomAction Installer.msi. The default project configuration is De.
2. 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 awork, 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
1. In the Windows Control Panel, double-click Add/Remove Programs.
2. In the Add/Remove Programs dialog box, select DBCustomAction Installer and click Remove, then click OK to close the dialog box.
Tip To uninstall from your development computer, on the Project menu, choose Uninstall.
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-958126/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 《關於工程帶資料庫打包(3)―自動安裝備份資料庫資料》 (轉)資料庫
- 《關於工程帶資料庫打包(2)―自動安裝定製的MSDE例項》 (轉)資料庫
- 關於移動資料庫(轉載)資料庫
- 關於資料庫open的深入探究(轉)資料庫
- 關於資料庫故障資料庫
- star帶你玩轉postgre資料庫_1_postgre安裝資料庫
- 3.3.1 關於關閉資料庫資料庫
- 【轉】關於Oracle資料庫的效能優化心得Oracle資料庫優化
- 關於打包
- 2.1 關於建立資料庫資料庫
- 關於資料庫碎片管理資料庫
- 關於資料庫驅動資料庫
- Oracle中關於資料庫例項名與資料庫服務名(轉載)Oracle資料庫
- Oracle資料庫關於SQL的執行計劃(轉)Oracle資料庫SQL
- 有關於SQL Server資料庫死鎖的分析(轉)SQLServer資料庫
- [轉]Mysql資料庫相關資料索引MySql資料庫索引
- 2.8.1.1 關於資料庫服務資料庫
- 關於資料庫連線池資料庫
- 關於資料庫字符集資料庫
- 關於資料庫取表的前N條記錄 (轉)資料庫
- Oracle 資料庫打包安裝Oracle資料庫
- 關聯式資料庫之父 (轉)資料庫
- 【轉載】關聯式資料庫還是NoSQL資料庫資料庫SQL
- 關於大資料和資料庫的討論大資料資料庫
- 2.5.1 關於建立資料庫的子句資料庫
- 關於資料庫鎖的總結資料庫
- 關於java連線SQL資料庫JavaSQL資料庫
- 閃回(關於閃回資料庫)資料庫
- 關於資料庫open的深入探究資料庫
- 關於建立DataGuard Physical Standby資料庫資料庫
- 關於資料庫檔案最大數資料庫
- 關係型資料庫:使用正規化建立資料庫(轉)資料庫
- 關於使用maven打包如何聚合資原始檔Maven
- 資料庫關閉的學問 (轉)資料庫
- 關於Oracle資料庫與MySQL資料庫的幾點區別Oracle資料庫MySql
- 關於資源庫
- 使用Kepserver 自帶 DataLogger 功能 實現工控資料轉儲關係型資料庫Server資料庫
- 關於oracle資料庫全形數字轉換半形數字方法Oracle資料庫