VB.NET是由微软推出的一款功能强大的,具有面向对象特性的编程语言。它可以帮助我们打造出一个安全稳定的编程环境,提高编程效率。我们将会在这篇文章为大家详细介绍一下有关VB.NET安装工程的相关概念。
刚听了SMART CLIENT部署的讲座,里面详细讲述了如果在VB.NET安装工程里运行自定义的DLL或者EXE,以后在自定义的工程中如何得到安装工程的传递过来的参数。下面大致说一下自己的心得:
首先,是制作一个类工程。然后添加一个安装类文件,并添加一个SQL.TXT文件,当作嵌入的资源,类的代码如下,在后面再做说明:
- Dim Cn As SqlConnection
- '得到SQL脚本字符串
- Private Function GetSql(ByVal Name As String) As String
- Try
- '通过反射取到程序集
- Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()
- '读取资源文件资料,要将资源文件在编译时设成嵌入的资源
- Dim Sm As Stream = Asm.GetManifestResourceStream
(Asm.GetName().Name + "." + Name)- Dim Rr As New StreamReader(Sm)
- Return Rr.ReadToEnd
- Catch ex As Exception
- MsgBox(Err.Description, MsgBoxStyle.OKOnly, "出错提示")
- Throw ex
- End Try
- End Function
- '得到数据库备份文件路径
- Private Function GetBakFilePath
(ByVal DatFileName As String) As String- Try
- '通过反射取到程序集
- Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()
- '读取资源文件资料,要将资源文件在编译时设成嵌入的资源
- Dim Sm As Stream = Asm.GetManifestResourceStream
(Asm.GetName().Name + "." + DatFileName)- If File.Exists("c:\" & DatFileName) Then
- File.Delete("c:\" & DatFileName)
- End If
- Dim Fs As New FileStream("c:\" & DatFileName, FileMode.Create)
- Dim Sw As New BinaryWriter(Fs)
- Dim recbyte(Sm.Length - 1) As Byte
- Dim strread As New BinaryReader(Sm)
- strread.Read(recbyte, 0, recbyte.Length)
- Sw.Write(recbyte, 0, recbyte.Length)
- Sw.Close()
- Fs.Close()
- Return "C:\" & DatFileName
- Catch ex As Exception
- MsgBox(Err.Description, MsgBoxStyle.OKOnly, "出错提示")
- Throw ex
- End Try
- End Function
- Private Sub ExecuteSql(ByVal DbName As String,
ByVal SqlText As String, ByVal uid As String,
ByVal pwd As String, ByVal servername As String)- Dim cmd As New SqlCommand(SqlText, Cn)
- If Cn.State = ConnectionState.Closed Then Cn.Open()
- Try
- Cn.ChangeDatabase(DbName)
- cmd.ExecuteNonQuery()
- Catch ex As Exception
- MsgBox("运行SQL脚本出错!", MsgBoxStyle.OKOnly, "提示")
- Throw ex
- End Try
- Cn.Close()
- End Sub
- Protected Sub AddDBTable(ByVal DBName As String,
ByVal uid As String, ByVal pwd As String, ByVal servername As String)- '判断数据库是否存在
- Try
- Cn = New SqlConnection("Persist Security Info=False;
User ID=" & uid & ";Initial Catalog=master;
Data Source=" & servername & ";password=" & pwd & ";Max Pool Size=75000")- Dim Dt As New DataTable()
- Cn.Open()
- Dim cmd As New SqlDataAdapter()
- cmd.SelectCommand = New SqlCommand("exec sp_helpdb", Cn)
- cmd.Fill(Dt)
- Dim i As Int16
- For i = 0 To Dt.Rows.Count - 1
- If DBName.ToLower = CType(Dt.Rows(i).Item(0), String).ToLower Then
- '数据库存在
- cmd = Nothing
- Cn.Close()
- Exit Sub
- End If
- Next
- Catch ex As Exception
- MsgBox(Err.Description & "连接数据库出错,请检查所输入的参数是否正确!",
MsgBoxStyle.OKOnly, "出错提示")- Exit Sub '如已存在则跳出
- End Try
- Try
- '========方法一:运行SQL脚本创建数据库
- ' ExecuteSql("Master", "Create DataBase " & DBName, uid, pwd, servername)
- 'ExecuteSql(DBName, GetSql("mysql.txt"), uid, pwd, servername)
- '========方法二:执行还原备份文件
- Dim cm As New ADODB.Command()
- Dim cn As New ADODB.Connection()
- Dim dbrs As New ADODB.Recordset()
- cn.Open("Provider=SQLOLEDB.1;Persist Security Info=False;
User ID=" & uid & ";Initial Catalog=master;
Data Source=" & servername & ";password=" & pwd)- cm.ActiveConnection = cn
- '判断该数据是否存在,存在就删除
- cm.CommandText = "exec sp_helpdb"
- dbrs = cm.Execute() '取所有数据库名
- If Not dbrs.BOF And Not dbrs.EOF Then
- Do While Not dbrs.EOF
- If Trim(dbrs.Fields(0).Value) = Trim(DBName) Then '如果该数据库存
- cm.CommandText = "drop database " & Trim(DBName)
- cm.Execute()
- Exit Do
- End If
- dbrs.MoveNext()
- Loop
- End If
- ' dbrs.Close()
- Dim Sfile As String = GetBakFilePath("installdb.dat")
- cm.CommandText = "restore database " & Trim(DBName) & " from DISK='" & Trim(Sfile) & "'"
- cm.Execute()
- cn.Close()
- If File.Exists(Sfile) Then
- File.Delete(Sfile)
- End If
- Catch ex As Exception
- MsgBox(Err.Description & "A", MsgBoxStyle.OKOnly, "出错提示")
- Throw ex
- End Try
- End Sub
- '覆写安装方法
- Public Overrides Sub Install(ByVal stateSaver As System.Collections.IDictionary)
- MyBase.Install(stateSaver)
- '取安装程序中用户界面中添回的文本框中的参数值
- Dim Connstr As String = Me.Context.Parameters.Item("dbname")
- Dim Cs As String() = Split(Connstr, "|")
- AddDBTable(Cs(0), Cs(1), Cs(2), Cs(3))
- End Sub
然后添加一个VB.NET安装工程,将上面的类的工程做为项目主输出添进来.在用户界面中加一个文本框#t#
然后在文本框的四个属性中输入参数名称..
这里设置了四个参数,在安装运行的时候,会有界面让你输入四个参数,在自定义操作里的安装项目上,将所加载进来的项目主输入选中置入.
并将属性CustomActionData设置为/参数名=[][][][]...以这种方式写入在文件框中设置好的参数,我们再看一下上面类中的代码:
- Dim Connstr As String = Me.Context.Parameters.Item("dbname")
便会取到参数dbname的值,亦就是在安装界面时要用户输入的值.
类中还覆写了Install方法.会自动运行,并执行用户自定义的程序代码.代码与平时所编的vbNET一样.
上面的VB.NET安装工程代码做了二种情况,一种是从嵌入的文本文件中取得SQL语句,运行它创建数据库.另一种是用SQL数据备份的DAT文件做为嵌入资源,在安装过程中,由用户输入的参数为条件,连接SQL服务器并还原数据库...