免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 969 | 回复: 0
打印 上一主题 下一主题

安装类:连接字符串修改及数据库创建 、授权开启 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:54 |只看该作者 |倒序浏览
看到网络上有关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不需要,由于时间关系只是未调整.
见图:



您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP