ALTER TABLE [dbo].[Employees] WITH NOCHECK ADD
CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED
(
[Name]
) ON [PRIMARY];
在“解决方案资源管理器”中,选择“sql.txt”。在“属性”窗口中,将“生成操作”属性设置为“嵌入的资源”。
将代码添加到安装程序类中以阅读文本文件
在“解决方案资源管理器”中,选择“DBCustomAction.vb”。在“视图”菜单上,选择“代码”。
在模块的顶部添加以下 Imports 语句:
Imports System.IO
Imports System.Reflection
将以下代码添加到类中:
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 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 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 Try
End Sub
Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
MyBase.Install(stateSaver)
AddDBTable(Me.Context.Parameters.Item("dbname"))
End Sub
在“生成”菜单上,选择“生成 DBCustomAction”。
创建部署项目
在解决方案资源管理器中选择“DBCustomAction Installer”项目。在“视图”菜单上指向“编辑器”,然后选择“用户界面”。
在用户界面编辑器中,选择“安装”下的“启动”节点。在“操作”菜单上,选择“添加对话框”。
在“添加对话框”对话框中,选择“文本框 (A)”对话框,然后单击“确定”关闭对话框。
在“操作”菜单上,选择“上移”。重复此步骤,直到“文本框 (A)”对话框位于“安装文件夹”节点之上。
在“属性”窗口中,选择 BannerText 属性并键入 Specify Database Name。
选择 BodyText 属性并键入 This dialog allows you to specify the name of the database to be created on the database server。
选择 Edit1Label 属性并键入 Name of DB:。
选择 Edit1Property 属性并键入 CUSTOMTEXTA1。
选择 Edit2Visible、Edit3Visible 和 Edit4Visible 属性,并将它们设置为 False。
创建自定义操作