大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇内容主要讲解“VB.NET安装工程的实现方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“VB.NET安装工程的实现方法”吧!
10年的响水网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整响水建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“响水网站设计”,“响水网站推广”以来,每个客户项目都认真落实执行。
写了很多关于VB.NET的代码了,在这里给大家提供一个VB.NET安装工程的技巧,希望大家可以从中找到价值。
首先,是制作一个类VB.NET安装工程。然后添加一个安装类文件,并添加一个SQL.TXT文件,当作嵌入的资源,类的代码如下,在后面再做说明。VB.NET安装工程代码:
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安装工程,将上面的类的工程做为项目主输出添进来.在用户界面中加一个文本框,然后在文本框的四个属性中输入参数名称..这里设置了四个参数,在安装运行的时候,会有界面让你输入四个参数,在自定义操作里的安装项目上,将所加载进来的项目主输入选中置入.并将属性CustomActionData设置为/参数名=[][][][]...以这种方式写入在文件框中设置好的参数,我们再看一下上面类中的代码:
Dim Connstr As String = Me.Context.Parameters.Item("dbname")
便会取到参数dbname的值,亦就是在安装界面时要用户输入的值.类中还覆写了Install方法.会自动运行,并执行用户自定义的程序代码.代码与平时所编的VBNET一样.上面的代码做了二种情况,一种是从嵌入的文本文件中取得SQL语句,运行它创建数据库.另一种是用SQL数据备份的DAT文件做为嵌入资源,在安装过程中,由用户输入的参数为条件,连接SQL服务器并还原数据库...
到此,相信大家对“VB.NET安装工程的实现方法”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!