剛聽了smart client部署的講座,里面詳細(xì)講述了如果在.net安裝工程里運(yùn)行自定義的dll或者exe,以后在自定義的工程中如何得到安裝工程的傳遞過來的參數(shù)。下面大致說一下自己的心得:
首先,是制作一個(gè)類工程。然后添加一個(gè)安裝類文件,并添加一個(gè)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í)設(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, "出錯(cuò)提示")
throw ex
end try
end function
'得到數(shù)據(jù)庫備份文件路徑
private function getbakfilepath(byval datfilename as string) as string
try
'通過反射取到程序集
dim asm as [assembly] = [assembly].getexecutingassembly()
'讀取資源文件資料,要將資源文件在編譯時(shí)設(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, "出錯(cuò)提示")
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("運(yùn)行sql腳本出錯(cuò)!", 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ù)庫出錯(cuò),請(qǐng)檢查所輸入的參數(shù)是否正確!", msgboxstyle.okonly, "出錯(cuò)提示")
exit sub '如已存在則跳出
end try
try
'========方法一:運(yùn)行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, "出錯(cuò)提示")
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
然后添加一個(gè)安裝工程,將上面的類的工程做為項(xiàng)目主輸出添進(jìn)來.在用戶界面中加一個(gè)文本框
然后在文本框的四個(gè)屬性中輸入?yún)?shù)名稱..
這里設(shè)置了四個(gè)參數(shù),在安裝運(yùn)行的時(shí)候,會(huì)有界面讓你輸入四個(gè)參數(shù),在自定義操作里的安裝項(xiàng)目上,將所加載進(jìn)來的項(xiàng)目主輸入選中置入.
并將屬性customactiondata設(shè)置為/參數(shù)名=[][][][]...以這種方式寫入在文件框中設(shè)置好的參數(shù),我們?cè)倏匆幌律厦骖愔械拇a:
dim connstr as string = me.context.parameters.item("dbname")
便會(huì)取到參數(shù)dbname的值,亦就是在安裝界面時(shí)要用戶輸入的值.
類中還覆寫了install方法.會(huì)自動(dòng)運(yùn)行,并執(zhí)行用戶自定義的程序代碼.代碼與平時(shí)所編的vbnet一樣.
上面的代碼做了二種情況,一種是從嵌入的文本文件中取得sql語句,運(yùn)行它創(chuàng)建數(shù)據(jù)庫.另一種是用
sql數(shù)據(jù)備份的dat文件做為嵌入資源,在安裝過程中,由用戶輸入的參數(shù)為條件,連接sql服務(wù)器并還原數(shù)據(jù)庫...