打印本文 打印本文 关闭窗口 关闭窗口
打包,并自动安装SQL数据库
作者:武汉SEO闵涛  文章来源:敏韬网  点击数3575  更新时间:2007/11/14 13:00:50  文章录入:mintao  责任编辑:mintao
入到打包程序文件中,在程序组创建uninst.exe的快捷方式

附:installdb.vb类,要添加引用 system.configuration.install.dll :

Imports System.ComponentModel
Imports System.Configuration.Install

<RunInstaller(True)> Public Class Installer1
    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

    ''''注意: 以下过程是组件设计器所必需的
    ''''可以使用组件设计器来修改此过程。
    ''''不要使用代码编辑器来修改它。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
    End Sub

#End Region


    Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
        MyBase.Install(stateSaver)
        If Not InstallDB() Then
            ''''失败,反安装
            Me.Uninstall(stateSaver)
            Exit Sub
        End If
        DeleteFile(String.Format("{0}DB.dat", Me.Context.Parameters.Item("targetdir")))
    End Sub

    Public Overrides Sub Uninstall(ByVal stateSaver As System.Collections.IDictionary)
        ''''执行反安装
        MyBase.Uninstall(stateSaver)
        DeleteFile(String.Format("{0}DB.dat", Me.Context.Parameters.Item("targetdir")))
    End Sub

    Private Sub DeleteFile(ByVal paths As String)
        ''''删除指定的文件
        Try
            Dim delFile As New System.IO.FileInfo(paths)
            If delFile.Exists Then
                delFile.Delete()
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub CreateSql(ByVal paths As String)
        Dim File As System.IO.StreamWriter
        Dim db As String = String.Format("{0}", Me.Context.Parameters.Item("dbname"))
        Dim path As String = String.Format("{0}", Me.Context.Parameters.Item("targetdir"))
        Try
            Dim s As New System.Text.StringBuilder
            s.Append("use master" & vbCrLf)
            s.Append("" & vbCrLf)
            s.Append("if not exists (select * from sysdatabases where name=''''" & db & "'''')" & vbCrLf)
            s.Append(" BEGIN" & vbCrLf)
            s.Append("         create database " & db & vbCrLf)
            s.Append(" END" & vbCrLf)
            s.Append("" & vbCrLf)
            s.Append("if exists (select * from sysdevices where name=''''DBdisk'''')" & vbCrLf)
            s.Append(" BEGIN" & vbCrLf)
            s.Append("       EXEC sp_dropdevice ''''DBdisk''''" & vbCrLf)
            s.Append(" END" & vbCrLf)
            s.Append("Else" & vbCrLf)
            s.Append(" BEGIN" & vbCrLf)
            s.Append("       EXEC sp_addumpdevice ''''disk'''',''''DBdisk'''', ''''" & path & "DB.dat''''" & vbCrLf)
            s.Append(" END" & vbCrLf)
            s.Append("" & vbCrLf)
            s.Append("restore database " & db & vbCrLf)
            s.Append("from disk=''''" & path & "DB.dat''''" & vbCrLf)
            s.Append("with replace")
            File = New System.IO.StreamWriter(paths)
            File.Write(s.ToString)
        Catch ex As Exception
        Finally
            File.Close()
        End Try
    End Sub

    Private Function InstallDB() As Boolean
        ''''安装数据库,调用自动批处理。
        Try
            ''''创建临时脚本
            CreateSql(String.Format("{0}Mydb2000tp.sql", Me.Context.Parameters.Item("targetdir")))
     &n

上一页  [1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口