用vb製作登陸介面連接sql資料庫
先建一個模組在堶捫擗J下列內容:
Public conn As ADODB.Connection
Sub main()
Set conn = New ADODB.Connection
conn.C _
+ "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 |