安装打包程序的问题

liuzhonghe 2004-04-02 10:40:14
有一个web程序里面 有一个配置文件记录 sql server 登陆名+密码
现在我做了一个winform程序需要用户输入sql server 登陆名+密码
然后生成相应的连接,创建数据库,并修改web程序的配置文件,
现在需要在安装程序完成后能自动运行我的winform程序,看了一下sdk帮助
需要重载installer做一些操作,可是安装程序老是提示无 installstate,请问
该怎么做?
...全文
104 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuzhonghe 2004-04-03
  • 打赏
  • 举报
回复
问题是把这个.dll在安装程序里的自定义操作中设置为:主输出来自subinstaller(活动)后
运行安装程序总是提示未找到subinstaller.installstate文件,这是怎么回事?
liuzhonghe 2004-04-02
  • 打赏
  • 举报
回复
分不够可在加
bingeng 2004-04-02
  • 打赏
  • 举报
回复
Imports System.IO
Imports System.Reflection

Imports System
Imports System.Security
Imports System.Security.Permissions
Imports System.Collections
Imports Microsoft.VisualBasic


Imports System.ComponentModel
Imports System.Configuration.Install

<RunInstaller(True)> Public Class DBCreate
Inherits System.Configuration.Install.Installer

#Region " 组件设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是组件设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'Installer 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'组件设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是组件设计器所必需的
'可以使用组件设计器来修改此过程。
'不要使用代码编辑器来修改它。
Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
' Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
'
'SqlConnection1
'
' Me.SqlConnection1.ConnectionString = "workstation id=""UNIT-EYUO2OLD6J"";packet size=4096;integrated security=SSPI;data s" & _
' "ource=""UNIT-EYUO2OLD6J"";persist security info=False;initial catalog=master"

End Sub

#End Region


Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
MyBase.Install(stateSaver)

''''程序集物理路径
Dim Asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
Dim strPath As String = Left(Asm.Location, InStrRev(Asm.Location, "\") - 1)
strPath = Left(strPath, InStrRev(strPath, "\"))

'''''数据库连接字段
Dim strKey As String = "Data Source=127.0.0.1;Initial Catalog=Test;User ID=sa;Password=密码"
strKey = Replace(strKey, "sa", Me.Context.Parameters.Item("SqlUserID"))
strKey = Replace(strKey, "密码", Me.Context.Parameters.Item("SqlPWD"))

Dim strSqlDBName As String = Me.Context.Parameters.Item("SqlDBName")
''''恢复设备备份
' Dim sql As String = "RESTORE " + Me.Context.Parameters.Item("SqlDBName") + " FROM DISK = '" + strPath + "TestPro.BAK'"

Dim sql As String = "RESTORE DATABASE " + strSqlDBName + " " _
& "FROM DISK = '" + strPath + "TestPro.BAK' " _
& "WITH MOVE 'Test_Data' TO 'C:\" + strSqlDBName + "_data.mdf', " _
& "MOVE 'Test_log' TO 'C:\" + strSqlDBName + "_log.ldf'"

ExecuteSql(Replace(strKey, "Initial Catalog=Test;", ""), sql) '生成数据程序


'''''修改配置文件
Dim FileInfo As System.IO.FileInfo = New System.IO.FileInfo(strPath + "web.config")

If Not FileInfo.Exists Then
Throw New InstallException("没找到配置文件 web.config")
End If

Dim XmlDocument As New System.Xml.XmlDocument
XmlDocument.Load(FileInfo.FullName)


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 ' skip any comments
If Node.Attributes.GetNamedItem("key").Value = "External" Then
Node.Attributes.GetNamedItem("value").Value = Replace(strKey, "Test", Me.Context.Parameters.Item("SqlDBName"))
FoundIt = True
End If
End If
Next Node

If Not FoundIt Then '''
Throw New InstallException("Config file did not contain a ServerName section")
End If

XmlDocument.Save(FileInfo.FullName)

'''修改文件夹的属性为可写入,你可以不用
Dim f2 As New FileIOPermission(FileIOPermissionAccess.Read Or FileIOPermissionAccess.Write, strPath)
f2.AddPathList(FileIOPermissionAccess.Write Or FileIOPermissionAccess.Read, strPath)


End Sub


''''执行Sql语句
Private Sub ExecuteSql(ByVal Conn As String, ByVal Sql As String)

Dim oConn = New System.Data.SqlClient.SqlConnection
oConn.ConnectionString = Conn

Dim Command As New SqlClient.SqlCommand(Sql, oConn)

Command.CommandTimeout = 900

Command.Connection.Open()
''Command.Connection.ChangeDatabase(DatabaseName) '''为数据库连接更改数据库

Try
Command.ExecuteNonQuery()

Catch ex As Exception

Throw New InstallException("安装过程中出现错误: " & ex.Message)
Throw ex

Finally

Command.Connection.Close()

End Try
End Sub



''''在文本文件中提取Sql语句 参考部分
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
MsgBox("In GetSQL: " & ex.Message)
Throw ex
End Try

End Function

End Class
bingeng 2004-04-02
  • 打赏
  • 举报
回复
可以换一种方法来解决吗,
你可以先备份数据库,安装过程中恢复数据库,修改配置文件.我已经测试成功
可以在自定义操作中可以定义你的输入数据.

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧