首 页最新软件下载排行文章资讯投稿发布下载专题
维维下载站
您的位置:首页编程开发网络编程编程其它 → VB如何验证SQL数据表中的用户登录

VB如何验证SQL数据表中的用户登录

来源:维维整理 发布时间:2010-3-30 11:53:00 人气:

vb使用sql表中信息登录实例,窗体中两个textbox,一个是用户名:txtUsername,一个密码:txtPassword。两个CommandButton,一个是提交:subm,一个是退出。数据库表是:book数据库中的admin表。admin表中有username和password,怎样去验证用户的登录?当登录成功,载入窗体Form2。

将下边的代码复制到记事本里,另存为: 工程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

Private Sub cmdSubmit_Click()
If rs.State <> adStateClosed Then rs.Close
rs.Open "SELECT password FROM admin WHERE username='" & txtUsername.Text & "'"
If rs.EOF Then
MsgBox "用户不存在!", vbExclamation
Exit Sub
End If
If rs!password <> txtPassword.Text Then
MsgBox "密码错误!", vbCritical
Exit Sub
End If
Unload Me
Form2.Show
End Sub

再将下边的代码复制到记事本里,另存为 Form2.frm
'---------
VERSION 5.00
Begin VB.Form Form2
Caption = "Form2"
ClientHeight = 3090
ClientLeft = 60
ClientTop = 450
ClientWidth = 4680
LinkTopic = "Form2"
ScaleHeight = 3090
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
End
Attribute VB_Name = "Form2"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False


最后,将下边的代码复制到记事本里,另存为:Module1.bas
'--------
Attribute VB_Name = "Module1"
Global cn As New Connection
Global rs As New Recordset

Sub main()
cn.ConnectionString = "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=book;Data Source=."
On Error Resume Next
cn.Open
If Err.Number <> 0 Then
MsgBox Err.Description, vbOKOnly, "连接数据库出错,程序退出!"
Err.Clear
Else
rs.ActiveConnection = cn
Form1.Show
End If
End Sub

'----------
cn.ConnectionString = "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=book;Data Source=."
这一句的实际情况根据你自己的具体情况改一下:
Password= SQLServer 密码
User ID= SQLServer 用户名
Initial Catalog= 数据库
Data Source= 运行SQLServer的机器IP地址或机器名

相关下载
栏目导航
本类热门阅览