七).”InstallDB”安装程序类代码(vb.net)
Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.Collections.Generic
Imports System
Imports System.Data
Imports System.Data.SqlClient
Namespace InstallDB
Partial Public Class InstallerDB
Inherits Installer
''' ''' 必须的设计器变量
'''
''' 2011-4-27 10:20 by dan
Private components As System.ComponentModel.IContainer = Nothing
''' ''' 构造函数
'''
''' 2011-4-27 10:20 by dan
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Dim state As System.Collections.IDictionary = New Hashtable
'Add initialization code after the call to InitializeComponent
End Sub
''' ''' 清理所有正在使用的资源
'''
''' 2011-4-27 10:20 by dan
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not IsNothing(components) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#Region "组件设计器生成的代码"
''' ''' 设计器支持所需的方法——不要使用代码编辑器修改此方法的内容
'''
'''
Private Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
#Region "数据库文件为mdf和ldf文件"
''' ''' 附加数据库的mdf文件和ldf文件
'''
''' sql语句
''' 数据名
''' mdf文件名
''' ldf文件名
''' 2011-4-27 15:57 by dan
Private Sub CreateDataBase(ByVal strSql As String, ByVal DataName As String, ByVal strMdf As String, ByVal strLdf As String, ByVal path As String)
'附加数据库
Dim str As String
Dim myConn As SqlConnection = New SqlConnection(strSql)
str = "EXEC sp_attach_db @dbname='" & DataName & "',@filename1='" & strMdf & "',@filename2='" & strLdf & "'"
Dim myCommand As SqlCommand = New SqlCommand(str, myConn)
myConn.Open()
myCommand.ExecuteNonQuery()
myConn.Close()
End Sub
''' ''' 重写安装代码
'''
'''
''' 2011-5-7 18:46 by dan
Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
MyBase.Install(stateSaver)
Dim server As String = Me.Context.Parameters("server")
Dim uid As String = Me.Context.Parameters("user")
Dim pwd As String = Me.Context.Parameters("pwd")
Dim path As String = Me.Context.Parameters("targetdir")
'连接sql
Dim strSql As String = "server=" & server & ";uid=" & uid & ";pwd=" & pwd & ";database=master"
Dim DataName As String = "BookManage"
Dim strMdf As String = path & "/BookManage_Data.mdf"
Dim strLdf As String = path & "/BookManage_Log.ldf"
Me.CreateDataBase(strSql, DataName, strMdf, strLdf, path)
End Sub
#End Region
End Class
End Namespace