当前位置:编程学习 > VB >>

VB 怎样验证SQL数据表中的用户登录

窗体中两个textbox,一个是用户名:txtUsername,一个密码:txtPassword。

两个CommandButton,一个是提交:subm,一个是退出。

数据库表是:book数据库中的admin表。

admin表中有username和password

怎样去验证用户的登录?当登录成功,载入窗体Form2。

要具体的代码,十分感谢!

补充:数据库是SQL Server2000
		
答案:

先选用ADODC

'窗体1代码

Dim pnum As Integer

Private Sub Command1_Click()
                    Adodc1.RecordSource = "select * from Table_xxwhb where 用户名 ='" & Text1.Text & "'"
                    Adodc1.Refresh
                    If Adodc1.Recordset.RecordCount > 0 Then
                    '      Text2 = Mid(Md5_String_Calc(Text1), 1, 16)   '校对MD5
'''''''                    Txtpwd.Text = Mid(Md5_String_Calc(Txtpwd), 1, 32)
                    MPassword = Adodc1.Recordset.Fields("密码")
''                    user1 = Adodc1.Recordset.Fields("权限")
'                    user2 = Adodc1.Recordset.Fields("编号")
                   If Txtpwd.Text = MPassword Then                     '判断数据的密码是否正确
'                    UserName = Combo1.Text '用户名
'                    Userb = user2 '编号
'                    Usern = user1 '权限
                    Form2.Show
                    Unload Me
                  Else
                    If pnum < 3 Then 'pnum就是密码验证次数,当次数超过3次,系统会自动保护退出
                    pnum = pnum + 1
                    MsgBox "用户名或密码错误!", vbInformation, "错误次数:" & pnum
                   
                    'MsgBox "密码错误,请重新输入", , "登陆界面管理"
                    Txtpwd.Text = ""
                    Txtpwd.SetFocus
                    Else
                    MsgBox "密码错误次数超过上限,系统自动退出", , "登陆界面管理"
                    End
                   End If
                   End If
                   End If

 

'ADODC1中用的连接字符串

Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Data_RKGL;Data Source=(local)         '具体要看你那边了

         

把下边的代码复制到记事本里,另存为: 工程1.vbp
'--------------------------------------------
Type=Exe
Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\WINDOWS\system32\stdole2.tlb#OLE Automation
Reference=*\G{2A75196C-D9EB-4129-B803-931327F72D5C}#2.8#0#..\..\..\..\Program Files\Common Files\system\ado\msado15.dll#Microsoft ActiveX Data Objects 2.8 Library
Form=Form1.frm
Module=Module1; Module1.bas
Form=Form2.frm
Startup="Sub Main"
HelpFile=""
Command32=""
Name="工程1"
HelpContextID="0"
CompatibleMode="0"
MajorVer=1
MinorVer=0
RevisionVer=0
AutoIncrementVer=0
ServerSupportFiles=0
VersionCompanyName="1"
CompilationType=0
OptimizationType=0
FavorPentiumPro(tm)=0
CodeViewDebugInfo=0
NoAliasing=0
BoundsCheck=0
OverflowCheck=0
FlPointCheck=0
FDIVCheck=0
UnroundedFP=0
StartMode=0
Unattended=0
Retained=0
ThreadPerObject=0
MaxNumberOfThreads=1

[MS Transaction Server]
AutoRefresh=1


把下边的代码复制到记事本里,另存为: Form1.frm
'--------------------------------------------
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3090
ClientLeft = 60
ClientTop = 450
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.TextBox txtPassword
Height = 270
Left = 990
TabIndex = 3
Top = 1500
Width = 2895
End
Begin VB.TextBox txtUsername
Height = 270
Left = 990
TabIndex = 2
Top = 870
Width = 2895
End
Begin VB.CommandButton cmdExit
Caption = "退出"
Height = 495
Left = 2760
TabIndex = 1
Top = 2280
Width = 1455
End
Begin VB.CommandButton cmdSubmit
Caption = "登录"
Height = 495
Left = 360
TabIndex = 0
Top = 2280
Width = 1455
End
Begin VB.Label Label2
Caption = "密 码:"
Height = 285
Left = 210
TabIndex = 5
Top = 1500
Width = 735
End
Begin VB.Label Label1
Caption = "用户名:"
Height = 285
Left = 210
TabIndex = 4
Top = 870
Width = 735
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False


P

上一个:怎样把VC++代码转换成VB代码啊
下一个:求用VB做最大公约数和最小公倍数

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,