首页 > 编程 > .NET > 正文

使用VB.Net做一个配置web.config功能的WinForm

2024-07-10 13:01:13
字体:
来源:转载
供稿:网友
界面很简单,五个控件txtservername、cbodatabase、txtusername、txtpassword、cbwindowsauthentication

下面是vb.net后台代码:

imports system
imports system.diagnostics
imports system.configuration
imports system.xml
imports system.text
imports system.reflection

public class frmmain
??? inherits system.windows.forms.form

#region " windows 窗体设计器生成的代码 "

??? public sub new()
??????? mybase.new()

??????? '该调用是 windows 窗体设计器所必需的。
??????? initializecomponent()

??????? '在 initializecomponent() 调用之后添加任何初始化

??? end sub

??? '窗体重写 dispose 以清理组件列表。
??? protected overloads overrides sub dispose(byval disposing as boolean)
??????? if disposing then
??????????? if not (components is nothing) then
??????????????? components.dispose()
??????????? end if
??????? end if
??????? mybase.dispose(disposing)
??? end sub

??? 'windows 窗体设计器所必需的
??? private components as system.componentmodel.icontainer

??? '注意: 以下过程是 windows 窗体设计器所必需的
??? '可以使用 windows 窗体设计器修改此过程。
??? '不要使用代码编辑器修改它。
??? friend withevents groupbox1 as system.windows.forms.groupbox
??? friend withevents txtservername as system.windows.forms.textbox
??? friend withevents cbwindowsauthentication as system.windows.forms.checkbox
??? friend withevents grplogin as system.windows.forms.groupbox
??? friend withevents lblpassword as system.windows.forms.label
??? friend withevents lblusername as system.windows.forms.label
??? friend withevents txtpassword as system.windows.forms.textbox
??? friend withevents txtusername as system.windows.forms.textbox
??? friend withevents grbdatabase as system.windows.forms.groupbox
??? friend withevents cbodatabase as system.windows.forms.combobox
??? friend withevents btncancel as system.windows.forms.button
??? friend withevents btnok as system.windows.forms.button
??? friend withevents lblserver as system.windows.forms.label
??? private sub initializecomponent()
??????? me.groupbox1 = new system.windows.forms.groupbox
??????? me.txtservername = new system.windows.forms.textbox
??????? me.cbwindowsauthentication = new system.windows.forms.checkbox
??????? me.grplogin = new system.windows.forms.groupbox
??????? me.lblpassword = new system.windows.forms.label
??????? me.lblusername = new system.windows.forms.label
??????? me.txtpassword = new system.windows.forms.textbox
??????? me.txtusername = new system.windows.forms.textbox
??????? me.grbdatabase = new system.windows.forms.groupbox
??????? me.cbodatabase = new system.windows.forms.combobox
??????? me.btncancel = new system.windows.forms.button
??????? me.btnok = new system.windows.forms.button
??????? me.lblserver = new system.windows.forms.label
??????? me.groupbox1.suspendlayout()
??????? me.grplogin.suspendlayout()
??????? me.grbdatabase.suspendlayout()
??????? me.suspendlayout()
??????? '
??????? 'groupbox1
??????? '
??????? me.groupbox1.controls.add(me.txtservername)
??????? me.groupbox1.controls.add(me.cbwindowsauthentication)
??????? me.groupbox1.controls.add(me.grplogin)
??????? me.groupbox1.controls.add(me.grbdatabase)
??????? me.groupbox1.controls.add(me.btncancel)
??????? me.groupbox1.controls.add(me.btnok)
??????? me.groupbox1.controls.add(me.lblserver)
??????? me.groupbox1.location = new system.drawing.point(8, 8)
??????? me.groupbox1.name = "groupbox1"
??????? me.groupbox1.size = new system.drawing.size(321, 237)
??????? me.groupbox1.tabindex = 15
??????? me.groupbox1.tabstop = false
??????? me.groupbox1.text = "数据库设置"
??????? '
??????? 'txtservername
??????? '
??????? me.txtservername.location = new system.drawing.point(115, 30)
??????? me.txtservername.name = "txtservername"
??????? me.txtservername.size = new system.drawing.size(178, 21)
??????? me.txtservername.tabindex = 10
??????? me.txtservername.text = ""
??????? '
??????? 'cbwindowsauthentication
??????? '
??????? me.cbwindowsauthentication.location = new system.drawing.point(38, 60)
??????? me.cbwindowsauthentication.name = "cbwindowsauthentication"
??????? me.cbwindowsauthentication.size = new system.drawing.size(173, 22)
??????? me.cbwindowsauthentication.tabindex = 1
??????? me.cbwindowsauthentication.text = "使用windows验证"
??????? '
??????? 'grplogin
??????? '
??????? me.grplogin.controls.add(me.lblpassword)
??????? me.grplogin.controls.add(me.lblusername)
??????? me.grplogin.controls.add(me.txtpassword)
??????? me.grplogin.controls.add(me.txtusername)
??????? me.grplogin.location = new system.drawing.point(29, 65)
??????? me.grplogin.name = "grplogin"
??????? me.grplogin.size = new system.drawing.size(269, 73)
??????? me.grplogin.tabindex = 2
??????? me.grplogin.tabstop = false
??????? '
??????? 'lblpassword
??????? '
??????? me.lblpassword.location = new system.drawing.point(10, 43)
??????? me.lblpassword.name = "lblpassword"
??????? me.lblpassword.size = new system.drawing.size(76, 22)
??????? me.lblpassword.tabindex = 13
??????? me.lblpassword.text = "密码"
??????? me.lblpassword.textalign = system.drawing.contentalignment.middleright
??????? '
??????? 'lblusername
??????? '
??????? me.lblusername.location = new system.drawing.point(10, 17)
??????? me.lblusername.name = "lblusername"
??????? me.lblusername.size = new system.drawing.size(76, 26)
??????? me.lblusername.tabindex = 14
??????? me.lblusername.text = "用户"
??????? me.lblusername.textalign = system.drawing.contentalignment.middleright
??????? '
??????? 'txtpassword
??????? '
??????? me.txtpassword.location = new system.drawing.point(86, 43)
??????? me.txtpassword.name = "txtpassword"
??????? me.txtpassword.passwordchar = microsoft.visualbasic.chrw(42)
??????? me.txtpassword.size = new system.drawing.size(173, 21)
??????? me.txtpassword.tabindex = 4
??????? me.txtpassword.text = ""
??????? '
??????? 'txtusername
??????? '
??????? me.txtusername.location = new system.drawing.point(86, 17)
??????? me.txtusername.name = "txtusername"
??????? me.txtusername.size = new system.drawing.size(173, 21)
??????? me.txtusername.tabindex = 3
??????? me.txtusername.text = ""
??????? '
??????? 'grbdatabase
??????? '
??????? me.grbdatabase.controls.add(me.cbodatabase)
??????? me.grbdatabase.location = new system.drawing.point(29, 142)
??????? me.grbdatabase.name = "grbdatabase"
??????? me.grbdatabase.size = new system.drawing.size(269, 52)
??????? me.grbdatabase.tabindex = 5
??????? me.grbdatabase.tabstop = false
??????? me.grbdatabase.text = "数据库"
??????? '
??????? 'cbodatabase
??????? '
??????? me.cbodatabase.itemheight = 12
??????? me.cbodatabase.location = new system.drawing.point(14, 22)
??????? me.cbodatabase.name = "cbodatabase"
??????? me.cbodatabase.size = new system.drawing.size(240, 20)
??????? me.cbodatabase.tabindex = 6
??????? '
??????? 'btncancel
??????? '
??????? me.btncancel.location = new system.drawing.point(176, 202)
??????? me.btncancel.name = "btncancel"
??????? me.btncancel.size = new system.drawing.size(90, 25)
??????? me.btncancel.tabindex = 8
??????? me.btncancel.text = "&c取消"
??????? '
??????? 'btnok
??????? '
??????? me.btnok.location = new system.drawing.point(72, 202)
??????? me.btnok.name = "btnok"
??????? me.btnok.size = new system.drawing.size(90, 25)
??????? me.btnok.tabindex = 7
??????? me.btnok.text = "&o确定"
??????? '
??????? 'lblserver
??????? '
??????? me.lblserver.location = new system.drawing.point(29, 30)
??????? me.lblserver.name = "lblserver"
??????? me.lblserver.size = new system.drawing.size(77, 26)
??????? me.lblserver.tabindex = 9
??????? me.lblserver.text = "服务器"
??????? me.lblserver.textalign = system.drawing.contentalignment.middleright
??????? '
??????? 'frmmain
??????? '
??????? me.autoscalebasesize = new system.drawing.size(6, 14)
??????? me.clientsize = new system.drawing.size(336, 253)
??????? me.controls.add(me.groupbox1)
??????? me.name = "frmmain"
??????? me.groupbox1.resumelayout(false)
??????? me.grplogin.resumelayout(false)
??????? me.grbdatabase.resumelayout(false)
??????? me.resumelayout(false)

??? end sub

#end region

?

??? private sub frmmain_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
??????? dim xmldoc as xmldocument
??????? dim topm as xmlnodelist
??????? xmldoc = new xmldocument
??????? xmldoc.load("web.config")
??????? topm = xmldoc.documentelement.childnodes
??????? for each element as xmlelement in topm
??????????? if element.name.tolower() = "appsettings" then
??????????????? dim _node as xmlnodelist = element.childnodes
??????????????? if _node.count > 0 then
??????????????????? for each el as xmlelement in _node
??????????????????????? if el.attributes("key").innerxml = "servername" then _
??????????????????????????? txtservername.text = el.attributes("value").value
??????????????????????? if el.attributes("key").innerxml = "databasename" then _
??????????????????????????? cbodatabase.text = el.attributes("value").value
??????????????????????? if el.attributes("key").innerxml = "username" then _
???????????????????????????? txtusername.text = securityservice.symmetricdecrypt(el.attributes("value").value)
??????????????????????? if el.attributes("key").innerxml = "password" then _
??????????????????????????? txtpassword.text = securityservice.symmetricdecrypt(el.attributes("value").value)
??????????????????????? if el.attributes("key").innerxml = "windowsauthentication" then _
?????????????????????????? me.cbwindowsauthentication.checked = convert.toboolean(el.attributes("value").value)
??????????????????? next
??????????????? end if

??????????? end if
??????? next

??? end sub

??? '**********************************************************************************************
??? '** class:????????? getdatabases
??? '**
??? '** description:???
??? '**********************************************************************************************
??? private sub getdatabases()

??????? try

??????????? dim odatabase as new database
??????????? cbodatabase.beginupdate()
??????????? cursor.current = cursors.waitcursor

??????????? configuration.server = txtservername.text
??????????? configuration.database = "master" 'cbodatabase.text
??????????? configuration.password = txtpassword.text
??????????? configuration.username = txtusername.text
??????????? configuration.windowsauthentication = cbwindowsauthentication.checked


??????????? dim rd as sqlclient.sqldatareader
??????????? rd = odatabase.getdatabases
??????????? while rd.read()
??????????????? cbodatabase.items.add(rd.getstring(1))
??????????? end while
??????????? rd.close()
??????????? cbodatabase.sorted = true


??????? catch acomexception as system.runtime.interopservices.comexception

??????????? cbodatabase.datasource = nothing
??????????? cbodatabase.items.clear()
??????????? cbodatabase.text = ""

??????????? msgbox("could not connect to " & txtservername.text & ".", msgboxstyle.exclamation, _
??????????????? "sql connection error")
??????? catch e1 as exception
??????????? msgbox(e1.message)

??????? finally

??????????? cbodatabase.endupdate()
??????????? cursor.current = cursors.default

??????? end try

??? end sub

??? private sub btnok_click(byval sender as system.object, byval e as system.eventargs) handles btnok.click
??????? dim xmldoc as xmldocument
??????? dim topm as xmlnodelist
??????? xmldoc = new xmldocument
??????? xmldoc.load("web.config")
??????? topm = xmldoc.documentelement.childnodes
??????? for each element as xmlelement in topm
??????????? if element.name.tolower() = "appsettings" then
??????????????? dim _node as xmlnodelist = element.childnodes
??????????????? if _node.count > 0 then
??????????????????? for each el as xmlelement in _node
??????????????????????? if el.attributes("key").innerxml = "servername" then _
?????????????????????????? el.attributes("value").value = txtservername.text
??????????????????????? if el.attributes("key").innerxml = "databasename" then _
?????????????????????????? el.attributes("value").value = cbodatabase.text
??????????????????????? if el.attributes("key").innerxml = "username" then _
?????????????????????????? el.attributes("value").value = securityservice.symmetricencrypt(txtusername.text)
??????????????????????? if el.attributes("key").innerxml = "password" then _
?????????????????????????? el.attributes("value").value = securityservice.symmetricencrypt(txtpassword.text)
??????????????????????? if el.attributes("key").innerxml = "windowsauthentication" then _
????????????????????????? el.attributes("value").value = me.cbwindowsauthentication.checked.tostring()
??????????????????? next
??????????????? end if

??????????? end if
??????? next

??????? ' write xml to file
??????? xmldoc.save("web.config")
??????? me.close()

??? end sub

??? private sub cbwindowsauthentication_checkedchanged(byval sender as system.object, byval e as system.eventargs) handles cbwindowsauthentication.checkedchanged

??????? if cbwindowsauthentication.checked then
??????????? txtusername.text = ""
??????????? txtpassword.text = ""
??????????? txtusername.enabled = false
??????????? txtpassword.enabled = false
??????? else
??????????? txtusername.enabled = true
??????????? txtpassword.enabled = true
??????? end if

??????? configuration.windowsauthentication = cbwindowsauthentication.checked
??? end sub

??? private sub btncancel_click(byval sender as system.object, byval e as system.eventargs) handles btncancel.click
??????? if msgbox("are you sure you want to exit w/o configuring the database?", msgboxstyle.okcancel) = msgboxresult.ok then
??????????? me.close()
??????? end if
??? end sub
??? private sub txtpassword_lostfocus(byval sender as system.object, byval e as system.eventargs) handles txtpassword.lostfocus
??????? if cbwindowsauthentication.checked or (txtusername.text <> "" and txtpassword.text <> "") then
??????????? getdatabases()
??????? end if
??? end sub
end class


securityservice类是用来加/解密的,这样用户就无法直接从web.config中看到数据库用户和密码了。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表