如何用VB连接SQL数据库做登录

如何用VB连接SQL数据库做登录,第1张

VB连接SQL数据库,可通过ADO数据对象和SQL结构化查询语言实现。

ADO (ActiveX 数据对象), 这项新的数据访问技术的特性包括:更简单的对象模型;与其它 Microsoft 和非 Microsoft 

的技术更好的集成;为本地和远程数据数据提供的通用接口;可远程访问的和断开的记录集;用户可访问的数据绑定接口;以及层次结构的记录集。

结构化查询语言(Structured Query Language)简称SQL,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统。

代码示例:

Private Sub XPButton1_Click()

  On Error GoTo finish '防错代码,防止意外而导致的退出

  sql = "select * from 用户管理 where 用户名='" &Text1.Text &"' and 密码='" &Text2.Text &"'"

  cn.ConnectionString = "Provider=SQLOLEDB.1Persist Security Info=TrueUser ID=登录数据库用户名(默认为sa)Password=登录数据库密码Initial Catalog=数据库名Data Source=服务器名(默认为:MERRYCHINA)" '这是连接SQL数据库的语句

  cn.Open

  rs.CursorLocation = adUseClient

  rs.Open sql, cn, adOpenDynamic, adLockOptimistic

  '以上使用最通用的方法来查询数据库中是否有匹配的记录

  If rs.EOF = True Then '如果没有记录则说明用户或密码为错误的

      If pnum <2 Then 'pnum就是密码验证次数,当次数超过3次,系统会自动保护退出

          pnum = pnum + 1

          MsgBox "用户名或密码错误!", vbInformation, "错误次数:" &pnum

          rs.Close

          cn.Close

          Text1.Text = ""

          Text2.Text = ""

          Text1.SetFocus

          Exit Sub

      Else

          MsgBox "用户名或密码错误超过三次,系统会自动退出", vbInformation, "提示"

          End

      End If

  Else

      loginname = rs.Fields(0)

      Form1.Show

      rs.Close

      cn.Close

  End If

  Exit Sub

finish:

  MsgBox Err.Description

  rs.Close

  cn.Close

End Sub

注意 cn.ConnectionString此句需依据实际的SQL数据库建立连接获得的字符串。

给个邮箱,我发给你

Public start As New ADODB.Connection

Public action As New ADODB.Recordset

Private Sub Command1_Click()

Set action = start.Execute("select * from 密码 where 用户名='" &Text1.Text &"' and 密码='" &Text2.Text &"'")

If action.EOF = True Then

MsgBox "用户名或密码错误!", vbInformation, "警告"

Text1.Text = ""

Text2.Text = ""

Else

Unload Me

MsgBox "登录成功"

Form2.Show

End If

action.Close

End Sub

Private Sub Form_Load()

start.Open ("Provider=Microsoft.Jet.OLEDB.4.0Data Source=" &App.Path &"\lgmjb.mdb")

End Sub

界面设计:

1.ComboBox 名为: CboName 用来输入或显示用户名

2.TextBox 名为:TxtPassword 用来输入密码

3.TextBox 名为:TxtPasswordSure 在注册时用来输入确认密码

4.CheckBox 名为:ChkNew 用来新注册 Caption属性:新建用户

5.CommandButton 名为:CmdLoad 用来登陆或注册 Caption属性:登陆

6.CommandButton 名为:CmdExit 用以退出 Caption 属性:退出

7.Data 不需要多设置,这个控件不用的,只是第一次加载时起作用.Visible属性:False

代码如下:

Dim MyTable As TableDef

Dim MyField As Field

Dim MyDatabase As Database

Dim Myrs As Recordset

Dim StrRule As String

Private Sub CboName_Change()

If Len(CboName.Text) >8 Then

CboName.SelStart = 0

CboName.SelLength = 8

CboName.Text = CboName.SelText

CboName.SelStart = 8

End If

End Sub

Private Sub ChkNew_Click()

If ChkNew.Value = 0 Then

LblPasswordsure.Visible = False

TxtPasswordSure.Visible = False

CmdLoad.Caption = "登陆"

Else

LblPasswordsure.Visible = True

TxtPasswordSure.Visible = True

CmdLoad.Caption = "注册"

End If

CboName.SetFocus

End Sub

Private Sub CmdExit_Click()

End

End Sub

Private Sub CmdLoad_Click()

If CboName.Text = "" Then

MsgBox "请输入用户名!", vbExclamation, "友情提示"

CboName.SetFocus

Exit Sub

ElseIf TxtPassword.Text = "" Then

MsgBox "请输入密码!", vbExclamation, "友情提示"

TxtPassword.SetFocus

Exit Sub

ElseIf TxtPasswordSure.Text = "" And TxtPasswordSure.Visible = True Then

MsgBox "请输入确认密码!", vbExclamation, "友情提示"

TxtPasswordSure.SetFocus

Exit Sub

End If

If CmdLoad.Caption = "注册" Then

If TxtPasswordSure.Text <>TxtPassword.Text Then

MsgBox "密码和确认密码不同!", vbExclamation, "Sorry"

TxtPasswordSure.SetFocus

Exit Sub

End If

If CheckStr(CboName.Text) Then

MsgBox "用户名可以是中文,也可以是英文或数字,但其中不能带有" &vbNewLine &"像“" &StrRule &"”的字符!", vbExclamation, "友情提示"

CboName.SetFocus

Exit Sub

ElseIf CboName.Text = "user" Then

MsgBox "user是保留用字,不能用作用户名!", vbExclamation, "Sorry"

Exit Sub

End If

Set MyDatabase = Workspaces(0).OpenDatabase(App.Path &"\save")

Set Myrs = MyDatabase.OpenRecordset("select * from user where Name = " &Chr(34) &CboName.Text &Chr(34))

If Myrs.RecordCount >0 Then

MsgBox "用户已经存在", vbExclamation, "友情提示"

CboName.SetFocus

Exit Sub

End If

Set Myrs = MyDatabase.OpenRecordset("select * from user")

Myrs.AddNew

Myrs.Fields("name") = CboName.Text

Myrs.Fields("password") = TxtPassword.Text

Myrs.Fields("logontime") = Now

Myrs.Fields("Best") = "0"

Myrs.Fields("Run") = "0"

Myrs.Update

Myrs.Close

Set MyTable = MyDatabase.CreateTableDef(CboName.Text)

Set MyField = MyTable.CreateField("Result", 10, 4)

MyTable.Fields.Append MyField

Set MyField = MyTable.CreateField("RightRatio", 10, 6)

MyTable.Fields.Append MyField

Set MyField = MyTable.CreateField("TestTime", 10, 19)

MyTable.Fields.Append MyField

MyDatabase.TableDefs.Append MyTable

MsgBox "注册成功!", vbExclamation, "恭喜你"

CboName.AddItem (CboName.Text)

ChkNew.Value = 0

MyDatabase.Close

Else '登陆

If TxtPassword.Text <>TxtPasswordSure.Text And TxtPasswordSure.Visible = True Then

MsgBox "密码与确认密码不同,请重新输入!", vbExclamation, "友情提示"

TxtPassword.SetFocus

Exit Sub

End If

Set MyDatabase = Workspaces(0).OpenDatabase(App.Path &"\save")

Set Myrs = MyDatabase.OpenRecordset("select * from user where name =" &Chr(34) &CboName.Text &Chr(34))

If Myrs.RecordCount = 0 Then

MsgBox "用户不存在!", vbExclamation, "友情提示"

Exit Sub

End If

Myrs.MoveFirst

If TxtPassword.Text = Myrs.Fields("password") Then '登陆成功

Myrs.Close

Set Myrs = MyDatabase.OpenRecordset("select * from user")

If Myrs.BOF = False Then Myrs.MoveLast

If Myrs.BOF = False Then Myrs.MoveFirst

Myrs.MoveFirst

For i = 0 To Myrs.RecordCount - 1

Myrs.Edit

If Myrs.Fields("Name") = CboName.Text Then

Myrs.Fields("Run") = "1"

Else

Myrs.Fields("Run") = "0"

End If

Myrs.Update

Myrs.MoveNext

Next

'登陆成功,你加入要执行的 *** 作.

Unload Me'登陆窗口卸载

Else

Static WrongTime As Integer

WrongTime = WrongTime + 1

If WrongTime = 2 Then End

MsgBox "密码错误!", vbExclamation, "友情提示"

End If

Myrs.Close

MyDatabase.Close

End If

End Sub

Private Sub Form_Activate()

StrRule = "`~!@#$%^&*()_-+=|\:<,>.?/" &Chr(34) &Chr(39)

If Dir(App.Path &"\save.mdb", vbHidden) = "" Then '数据文件不见了

Set MyDatabase = CreateDatabase(App.Path &"\save", dbLangGeneral)

Set MyTable = MyDatabase.CreateTableDef("user")

Set MyField = MyTable.CreateField("Name", 10, 16)

MyTable.Fields.Append MyField

Set MyField = MyTable.CreateField("Password", 10, 6)

MyTable.Fields.Append MyField

Set MyField = MyTable.CreateField("LogonTime", 10, 19)

MyTable.Fields.Append MyField

Set MyField = MyTable.CreateField("Best", 10, 4)

MyTable.Fields.Append MyField

Set MyField = MyTable.CreateField("Run", 10, 1)

MyTable.Fields.Append MyField

MyDatabase.TableDefs.Append MyTable

ChkNew.Value = 2

Else

Set MyDatabase = Workspaces(0).OpenDatabase(App.Path &"\save")

Set Myrs = MyDatabase.OpenRecordset("select * from user")

If Myrs.EOF = False Then Myrs.MoveLast

If Myrs.BOF = False Then Myrs.MoveFirst

Dim i As Integer

Dim ShowIndex As Integer

For i = 0 To Myrs.RecordCount - 1

CboName.AddItem (Myrs.Fields("Name"))

If Myrs.Fields("Run") = "1" Then ShowIndex = i

Myrs.MoveNext

Next

If CboName.ListCount >0 Then

CboName.ListIndex = ShowIndex

Else

ChkNew.Value = 2

End If

Myrs.Close

MyDatabase.Close

End If

TxtPassword.SetFocus

End Sub

Private Function CheckStr(StrTheword As String) As Boolean

Dim i As Integer

For i = 1 To Len(StrRule)

If InStr(1, StrTheword, Mid(StrRule, i, 1)) <>0 Then '含有这样的字符

CheckStr = True

Exit Function

End If

Next

CheckStr = False

End Function

'你要源文件可以发EMail给我索要.

'我的电邮地址是:wangaochao@163.com


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10039122.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存