剛聽了smart client部署的講座,里面詳細講述了如果在.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
然后添加一個安裝工程,將上面的類的工程做為項目主輸出添進來.在用戶界面中加一個文本框
然后在文本框的四個屬性中輸入參數名稱..
這里設置了四個參數,在安裝運行的時候,會有界面讓你輸入四個參數,在自定義操作里的安裝項目上,將所加載進來的項目主輸入選中置入.
并將屬性customactiondata設置為/參數名=[][][][]...以這種方式寫入在文件框中設置好的參數,我們再看一下上面類中的代碼:
dim connstr as string = me.context.parameters.item("dbname")
便會取到參數dbname的值,亦就是在安裝界面時要用戶輸入的值.
類中還覆寫了install方法.會自動運行,并執行用戶自定義的程序代碼.代碼與平時所編的vbnet一樣.
上面的代碼做了二種情況,一種是從嵌入的文本文件中取得sql語句,運行它創建數據庫.另一種是用
sql數據備份的dat文件做為嵌入資源,在安裝過程中,由用戶輸入的參數為條件,連接sql服務器并還原數據庫...