看到网络上有关web安装项目详细方法的较少,且创建数据库\连接字符串\Windows集成安全都比较常用,现贴出VB.net代码,适当修改都可通用.希望对大家有帮助. 以下为三个Installer Class的具体代码,由于工作时间紧,有些imports是不需要的.
执行SQL创建Database的代码:
Imports System.ComponentModel Imports System.Configuration.Install Imports System.IO Imports System.Reflection Imports System.Data.SqlClient Imports System.Web Imports System.Runtime.InteropServices Imports System.DirectoryServices Imports System.IO.DirectoryInfo
Public Class NDSInstaller Dim masterConnection As New System.Data.SqlClient.SqlConnection Dim str As String
Public Sub New() MyBase.New() InitializeComponent() End Sub Private Function GetSql(ByVal Name As String) As String Try Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly() Dim strm As Stream = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name) Dim reader As StreamReader = New StreamReader(strm) Return reader.ReadToEnd() Catch ex As Exception Throw ex End Try End Function
Private Sub ExecuteSql(ByVal DatabaseName As String, ByVal Sql As String) Dim Command As New SqlClient.SqlCommand(Sql, masterConnection) str = "Data Source=" + Me.Context.Parameters.Item("server") + ";Initial Catalog=" + DatabaseName + ";Persist Security Info=True;User ID=" + Me.Context.Parameters.Item("username") + ";Password=" + Me.Context.Parameters.Item("password") masterConnection.ConnectionString = str Command.Connection.Open() Command.Connection.ChangeDatabase(DatabaseName) Try Command.ExecuteNonQuery() Finally 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 Throw ex End Try End Sub
Public Overrides Sub Install(ByVal stateSaver As _ System.Collections.IDictionary) MyBase.Install(stateSaver) AddDBTable(Me.Context.Parameters.Item("database")) End Sub End Class 修改Web.Config的代码: Imports System.ComponentModel Imports System.Configuration.Install Imports System.IO Imports System.Reflection Imports System.Data.SqlClient Imports System.Web Imports System.Runtime.InteropServices Imports System.IO.DirectoryInfo
Public Class ConfInstaller Dim str As String Dim gpStr As String Dim cpStr As String Public Sub New() MyBase.New()
'This call is required by the Component Designer. InitializeComponent()
'Add initialization code after the call to InitializeComponent
End Sub Public Overrides Sub Install(ByVal stateSaver As _ System.Collections.IDictionary) MyBase.Install(stateSaver) Try 'Setup web.config Str = "Data Source=" + Me.Context.Parameters.Item("server") + ";Initial Catalog=" + Me.Context.Parameters.Item("database") + ";Persist Security Info=True;User ID=" + Me.Context.Parameters.Item("username") + ";Password=" + Me.Context.Parameters.Item("password") gpStr = "Data Source=" + Me.Context.Parameters.Item("gpserver") + ";Initial Catalog=" + Me.Context.Parameters.Item("gpdatabase") + ";Persist Security Info=True;User ID=" + Me.Context.Parameters.Item("gpusername") + ";Password=" + Me.Context.Parameters.Item("gppassword") cpStr = "Data Source=" + Me.Context.Parameters.Item("gpserver") + ";Initial Catalog=DBNAME;Persist Security Info=True;User ID=" + Me.Context.Parameters.Item("gpusername") + ";Password=" + Me.Context.Parameters.Item("gppassword") Dim xmlDoc As Xml.XmlDocument = New Xml.XmlDocument() xmlDoc.Load(Me.Context.Parameters("path") + "web.config")
'NDS Portal Connection String Dim node As Xml.XmlNodeList = xmlDoc.SelectNodes("//connectionStrings/add[@name='NDSPortalConnectionString1']") Dim ele As Xml.XmlElement = node.ItemOf(0) ele.SetAttribute("connectionString", Str)
'Dynamics GP Connection String Dim gpNode As Xml.XmlNodeList = xmlDoc.SelectNodes("//connectionStrings/add[@name='GPConnectionString']") Dim gpele As Xml.XmlElement = gpNode.ItemOf(0) gpele.SetAttribute("connectionString", gpStr)
'Company Connection String Dim cpNode As Xml.XmlNodeList = xmlDoc.SelectNodes("//connectionStrings/add[@name='TWOConnectionString']") Dim cpele As Xml.XmlElement = cpNode.ItemOf(0) cpele.SetAttribute("connectionString", cpStr) 'Save xml xmlDoc.Save(Me.Context.Parameters("path") + "web.config") Catch ex As Exception Throw ex End Try End Sub End Class 设定Web Site为Windows集成安全的代码: Imports System.ComponentModel Imports System.Configuration.Install Imports System.IO Imports System.Reflection Imports System.Data.SqlClient Imports System.Web Imports System.Runtime.InteropServices Imports System.DirectoryServices Imports System.IO.DirectoryInfo
Public Class SecInstaller
Public Sub New() MyBase.New()
'This call is required by the Component Designer. InitializeComponent()
'Add initialization code after the call to InitializeComponent
End Sub Public Overrides Sub Install(ByVal stateSaver As _ System.Collections.IDictionary) MyBase.Install(stateSaver) Try 'Setup windows auth Dim vDir As String = Me.Context.Parameters("path") 'MsgBox("0") Dim dirInfo As DirectoryInfo = New DirectoryInfo(vDir) 'MsgBox("1") Dim dir As DirectoryEntry = New DirectoryEntry("IIS://localhost/W3SVC/1/Root/" + dirInfo.Name) 'MsgBox("2") dir.InvokeSet("AuthFlags", AuthenticationTypes.ReadonlyServer) 'MsgBox("3") dir.CommitChanges() Catch ex As Exception
End Try End Sub End Class 注意点: 1.在第一个项目中需要加入一个sql.txt文件,为嵌入式的资源. 2.加入Project Output后需要设定执行条件与参数的传递字串. 3.部分Imports不需要,由于时间关系只是未调整. 见图:   |