'' ------------------------建立数据库-------------------------------------------------
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"))
''根据输入的数据库名称建立数据库
executesql(connstr, "master", "CREATE DATABASE " + Me.Context.Parameters.Item("dbname"))
''调用osql执行脚本
dim sqlProcess As New System.Diagnostics.Process
sqlprocess.startinfo.filename = "osql.exe "
sqlprocess.startinfo.arguments = 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() ''等待执行
sqlprocess.close()
''删除脚本文件
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 ex As Exception
throw ex
end Try
'' ---------------------将连接字符串写入Web.config-----------------------------------
try
dim FileInfo As System.IO.FileInfo = New System.IO.FileInfo(Me.Context.Parameters.Item("targetdir") & "\web.config")
if Not FileInfo.Exists Then
throw New InstallException("没有找到配置文件")
end If
''实例化xml文档
dim XmlDocument As New System.Xml.XmlDocument
xmldocument.load(fileinfo.fullname)
''查找到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
''写入连接字符串
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 Not FoundIt Then
throw New InstallException("web.Config 文件没有包含connString连接字符串设置")
end If
xmldocument.save(fileinfo.fullname)
catch ex As Exception
throw ex
end Try
end Sub
end Class
最后编译生成!
安装界面:
说明:本文版权与解释权归李洪根所有,如需转载,请保留完整的内容及此声明。
QQ: 21177563 MSN: lihonggen@hotmail.com