您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何解析VB.NET安裝工程具體應(yīng)用方法,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
VB.NET是由微軟推出的一款功能強大的,具有面向?qū)ο筇匦缘木幊陶Z言。它可以幫助我們打造出一個安全穩(wěn)定的編程環(huán)境,提高編程效率。我們將會在這篇文章為大家詳細(xì)介紹一下有關(guān)VB.NET安裝工程的相關(guān)概念。
剛聽了SMART CLIENT部署的講座,里面詳細(xì)講述了如果在VB.NET安裝工程里運行自定義的DLL或者EXE,以后在自定義的工程中如何得到安裝工程的傳遞過來的參數(shù)。下面大致說一下自己的心得:
首先,是制作一個類工程。然后添加一個安裝類文件,并添加一個SQL.TXT文件,當(dāng)作嵌入的資源,類的代碼如下,在后面再做說明:
Dim Cn As SqlConnection
'得到SQL腳本字符串
Private Function GetSql(ByVal Name As String) As String
Try
'通過反射取到程序集
Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()
'讀取資源文件資料,要將資源文件在編譯時設(shè)成嵌入的資源
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
'得到數(shù)據(jù)庫備份文件路徑
Private Function GetBakFilePath
(ByVal DatFileName As String) As StringTry
'通過反射取到程序集
Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly()
'讀取資源文件資料,要將資源文件在編譯時設(shè)成嵌入的資源
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)'判斷數(shù)據(jù)庫是否存在
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
'數(shù)據(jù)庫存在
cmd = Nothing
Cn.Close()
Exit Sub
End If
Next
Catch ex As Exception
MsgBox(Err.Description & "連接數(shù)據(jù)庫出錯,請檢查所輸入的參數(shù)是否正確!",
MsgBoxStyle.OKOnly, "出錯提示")Exit Sub '如已存在則跳出
End Try
Try
'========方法一:運行SQL腳本創(chuàng)建數(shù)據(jù)庫
' ExecuteSql("Master", "Create DataBase " & DBName, uid, pwd, servername)
'ExecuteSql(DBName, GetSql("mysql.txt"), uid, pwd, servername)
'========方法二:執(zhí)行還原備份文件
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
'判斷該數(shù)據(jù)是否存在,存在就刪除
cm.CommandText = "exec sp_helpdb"
dbrs = cm.Execute() '取所有數(shù)據(jù)庫名
If Not dbrs.BOF And Not dbrs.EOF Then
Do While Not dbrs.EOF
If Trim(dbrs.Fields(0).Value) = Trim(DBName) Then '如果該數(shù)據(jù)庫存
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)
'取安裝程序中用戶界面中添回的文本框中的參數(shù)值
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安裝工程,將上面的類的工程做為項目主輸出添進(jìn)來.在用戶界面中加一個文本框
VB.NET控件自動排序相關(guān)代碼示例指南
VB.NET查詢包含威力強大
VB.NET擴展方法概念詳解
VB.NET局部變量類型推測基本概念剖析
VB.NET對象序列劇本概念剖析
然后在文本框的四個屬性中輸入?yún)?shù)名稱..
這里設(shè)置了四個參數(shù),在安裝運行的時候,會有界面讓你輸入四個參數(shù),在自定義操作里的安裝項目上,將所加載進(jìn)來的項目主輸入選中置入.
并將屬性CustomActionData設(shè)置為/參數(shù)名=[][][][]...以這種方式寫入在文件框中設(shè)置好的參數(shù),我們再看一下上面類中的代碼:
Dim Connstr As String = Me.Context.Parameters.Item("dbname")
便會取到參數(shù)dbname的值,亦就是在安裝界面時要用戶輸入的值.
類中還覆寫了Install方法.會自動運行,并執(zhí)行用戶自定義的程序代碼.代碼與平時所編的vbNET一樣.
上面的VB.NET安裝工程代碼做了二種情況,一種是從嵌入的文本文件中取得SQL語句,運行它創(chuàng)建數(shù)據(jù)庫.另一種是用SQL數(shù)據(jù)備份的DAT文件做為嵌入資源,在安裝過程中,由用戶輸入的參數(shù)為條件,連接SQL服務(wù)器并還原數(shù)據(jù)庫。
關(guān)于如何解析VB.NET安裝工程具體應(yīng)用方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。