舉例數據庫為SqlUsers,用戶表為:Users,用戶名字段:UserName,用戶密碼字段:UserPassword
Public Rs As New ADODB.Recordset Public Conn As New ADODB.Connection Public Sub ConnOpen() '打開數據庫 On Error GoTo ConnOpenError Conn.ConnectionString = "driver={sql server};server=(local);uid=sa;pwd=123456;database=SqlUser" Conn.Open Exit Sub ConnOpenError: MsgBox Err.Description End Sub Public Sub ConnClose() '關閉數據庫 On Error GoTo ConnCloseError If Conn.State = 1 Then Conn.Close Set Conn = Nothing End If Exit Sub ConnCloseError: MsgBox Err.Description End Sub
Private Sub Command1_Click() If CmbName.Text = "" Then MsgBox "對不起,請選擇用戶名!", vbInformation Exit Sub ElseIf txtPwd.Text = "" Then MsgBox "對不起,請輸入舊密碼", vbInformation Exit Sub ElseIf txtXinPwd.Text = "" Then MsgBox "對不起,請輸入新密碼", vbInformation Exit Sub End If Rs.Open "Select * From Users Where UserName = '" & CmbName.Text & "'", Conn, 1, 3 If Rs.EOF And Rs.BOF Then MsgBox "對不起,用戶名不存在!", vbInformation ElseIf txtPwd.Text <> Rs("UserPassword") Then MsgBox "對不起,舊密碼錯誤!", vbInformation Else Rs("UserPassword") = txtXinPwd.Text Rs.Update End If Rs.Close End Sub
Private Sub Form_Load() ConnOpen
End Sub
Private Sub Form_Unload(Cancel As Integer) ConnClose End Sub
|
| |