vb用户登录界面,用户登陆的代码,连接SQL Server数据库的

如题所述

第1个回答  推荐于2018-03-13
Option Explicit
Private Function Selectsql(SQL As String) As ADODB.Recordset '返回ADODB.Recordset对象
Dim ConnStr As String
Dim Conn As ADODB.Connection
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Set Conn = New ADODB.Connection

'On Error GoTo MyErr:
ConnStr = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=001234;Initial Catalog=Mydatabase;Data Source=MERRYCHINA" '这是连接SQL数据库的语句
Conn.Open ConnStr
rs.CursorLocation = adUseClient
rs.Open Trim$(SQL), Conn, adOpenDynamic, adLockOptimistic
Set Selectsql = rs
'Exit Function
'MyErr:
'Set rs = Nothing
'Set Conn = Nothing '释放相关的系统资源
'MsgBox Err.Description, vbInformation, "系统提示" '显示出错信息
End Function
Private Sub Form_Load()
Dim SQL As String
Dim rs As ADODB.Recordset
Dim X As Long
On Error GoTo Err_box
SQL = " select * from A用户表 ORDER BY ID"
Set rs = Selectsql(SQL)
If rs.RecordCount > 0 Then
rs.MoveFirst
For X = 1 To rs.RecordCount
Combo1.AddItem rs.Fields("姓名").Value
rs.MoveNext
Next X
Combo1.ListIndex = 0
End If
rs.Close
Exit Sub
Err_box:
End Sub
Private Sub Command1_Click()
Dim SQL As String
Dim rs As ADODB.Recordset
If Text1.Text = "" Then
MsgBox "请输入口令!", 64, "提示"
Text1.SetFocus
Exit Sub
End If
If Combo1.Text = "" Then
MsgBox "请选择帐号!", 64, "提示"
Combo1.SetFocus
Exit Sub
End If
SQL = "SELECT * FROM A用户表 WHERE 姓名='" & Combo1.Text & "' AND 密码='" & Text1.Text & "' "
Set rs = Selectsql(SQL)
If rs.RecordCount > 0 Then
Form1.Show
Unload Me
Else
MsgBox "口令不对,请重新输入!", 64, "提示"
Text1.SetFocus
End If
End Sub
'**********************************************************************
'说明:1) 在工程中引用Microsoft ActiveX Data Objects 2.8 Library ,其它版本也行如:2.0
' 2) 在窗体中加Texe1.text(文本框控件),Combo1.text(组合框控件),Command1(命令按钮)各一个
' 3) 在SQL Server2000中创建数据库"MyDatabase",新建表"A用户表",表中包含"ID,姓名,密码"等字段,然后将以上代码复制,OK搞定本回答被提问者和网友采纳
第2个回答  推荐于2018-05-03

vb登陆程序源代码

你可以这样做建一个模块在里面输入下列
Public conn As ADODB.Connection
Sub main()
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;" _
+ "User ID=sa;password=sa;Initial Catalog=您的数据库名;Data Source=127.0.0.1"
conn.Open
from1.Show ’登录界面
End Sub

再在登录界面“确定”下写入如下代码:
Private Sub Command1_Click()
If id.Text = "" Then
MsgBox "用户名不能为空!", vbOKOnly + vbInformation, "友情提示"
id.SetFocus
Exit Sub
End If
If password.Text = "" Then
MsgBox "密码不能为空!", vbOKOnly + vbInformation, "友情提示"
password.SetFocus
Exit Sub
End If

Dim strSQl As String
strSQl = "select * from Users where users_name='" & Trim$(id.Text) & "' and password='" & Trim$(password.Text) & "' "

Dim str As New ADODB.Recordset
Set str = New ADODB.Recordset
str.CursorLocation = adUseClient
str.Open strSQl, conn, adOpenStatic, adLockReadOnly

With str
If .State = adStateOpen Then .Close
.Open strSQl
If .EOF Then
Try_times = Try_times + 1
If Try_times >= 3 Then
MsgBox "您已经三次尝试进入本系统,均不成功,系统将自动关闭", vbOKOnly + vbCritical, "警告"
Unload Me
Else
MsgBox "对不起,用户名不存在或密码错误 !", vbOKOnly + vbQuestion, "警告"
id.SetFocus
id.Text = ""
password.Text = ""
End If
Else

Unload Me

Form2.Show ’登录进入的另一个界面

End If
End With

End Sub本回答被网友采纳
相似回答