您好,登錄后才能下訂單哦!
本篇內容主要講解“VB.NET安裝工程的實現方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“VB.NET安裝工程的實現方法”吧!
寫了很多關于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安裝工程的實現方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。