您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何解析VB.NET安裝工程具體應用方法,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
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 StringTry
'通過反射取到程序集
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安裝工程,將上面的類的工程做為項目主輸出添進來.在用戶界面中加一個文本框
VB.NET控件自動排序相關代碼示例指南
VB.NET查詢包含威力強大
VB.NET擴展方法概念詳解
VB.NET局部變量類型推測基本概念剖析
VB.NET對象序列劇本概念剖析
然后在文本框的四個屬性中輸入參數名稱..
這里設置了四個參數,在安裝運行的時候,會有界面讓你輸入四個參數,在自定義操作里的安裝項目上,將所加載進來的項目主輸入選中置入.
并將屬性CustomActionData設置為/參數名=[][][][]...以這種方式寫入在文件框中設置好的參數,我們再看一下上面類中的代碼:
Dim Connstr As String = Me.Context.Parameters.Item("dbname")
便會取到參數dbname的值,亦就是在安裝界面時要用戶輸入的值.
類中還覆寫了Install方法.會自動運行,并執行用戶自定義的程序代碼.代碼與平時所編的vbNET一樣.
上面的VB.NET安裝工程代碼做了二種情況,一種是從嵌入的文本文件中取得SQL語句,運行它創建數據庫.另一種是用SQL數據備份的DAT文件做為嵌入資源,在安裝過程中,由用戶輸入的參數為條件,連接SQL服務器并還原數據庫。
關于如何解析VB.NET安裝工程具體應用方法就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。