csdn论坛中vb版里经常见到一些朋友问到关于登陆的问题,这篇文章专门为初学的朋友提供一些帮助,当然希望有更好办法的高手给予指点。
首先 文件→新建→项目 选择windows应用程序
并在窗体上添加2个label控件、2个textbox控件、2 个buton控件如下图(1-1):
界面设计好后在button的click事件中写入代码如下:
(在程序顶部引用imports system.data.oledb)
private sub bok_click(byval sender as system.object, byval e as system.eventargs) handles bok.click
定义
dim olecn as new oledbconnection("provider=microsoft.jet.oledb.4.0;data source=data.mdb")
dim olecm as new oledbcommand("select * from login", olecn)
dim olead as new oledbdataadapter
dim olerd as oledbdatareader
dim pd as boolean = false 定义boolean为后面错误处理使用
dim i as integer
olecm = new oledbcommand("select * from login", olecn)
与数据库建立连接
olecn.open()
olerd = olecm.executereader
循环判断与数据库中数据是否相同
do while olerd.read()
for i = 0 to olerd.fieldcount – 1
if trim(tuser.text) = (olerd.item("帐号")) and trim(tpassword.text) = (olerd.item("密码")) then
msgbox("成功登陆")
pd = true
exit sub
end if
next
loop
tuser.text = ""
tpassword.text = ""
tuser.focus()
olerd.close()
olecn.close()
end sub
整个过程就做完了简单吧。
