刚听了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服务器并还原数据库...