0

我有 2 个表格

第一种形式有

Public Class frmMain

Friend strBillIDNumber As String
Friend strBillPassword As String
Friend ds As New DataSet
Friend cnn As OleDbConnection
Friend sql As String
Friend adptr As OleDbDataAdapter
Dim attempt As Integer = 1

Private Sub btnSign_Click(sender As Object, e As EventArgs) Handles btnSign.Click

    Dim connectionString As String
    connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Renz\Documents\Visual Studio 2012\FINAL\Database\UsersDB.accdb;"
    sql = "Select ID, LASTNAME, FIRSTNAME, LOGINSTATUS from TblUser"
    cnn = New OleDbConnection(connectionString)
    Try
        cnn.Open()
        adptr = New OleDbDataAdapter(sql, cnn)
        adptr.Fill(ds)



        For i = 0 To ds.Tables(0).Rows.Count - 1

            If ds.Tables(0).Rows(i).Item(0) = txtID.Text Then
                MessageBox.Show("UserName Matched")
                If ds.Tables(0).Rows(i).Item(1) = txtPassword.Text Then
                    MessageBox.Show("Password Matched")

                    If txtPassword.Text = "admin" Then
                        MessageBox.Show("You are now Logged In as Admin.")
                        frmFaculty.Show()
                        Me.Hide()
                    Else
                        frmStudent.lblSI.Text = ds.Tables(0).Rows(i).Item(0)
                        frmStudent.lblLN.Text = ds.Tables(0).Rows(i).Item(1)
                        frmStudent.lblFN.Text = ds.Tables(0).Rows(i).Item(2)
                        frmStudent.lblLS.Text = ds.Tables(0).Rows(i).Item(3)
                        MessageBox.Show("You are now Logged In.")
                        frmStudent.Show()
                        Me.Hide()
                    End If
                Else
                    MessageBox.Show("Invalid Password.")
                    MessageBox.Show("Please Try Again." & vbNewLine & "ATTEMPT: " & attempt & " out of 3")
                    attempt = attempt + 1
                End If
            End If

        Next

        adptr.Dispose()
        cnn.Close()
    Catch ex As Exception
        MessageBox.Show("Please Try Again!")
    End Try

    'log-in attempt 3x fail'
    If attempt > 3 Then
        MessageBox.Show("You have exceeded the number of login attempt." & vbNewLine & "Please Contact the Administrator.")
    End If
End Sub

我的第二种形式有

Imports System.Data.OleDb

Public Class frmStudent
Dim TimeInHold As String
Private Sub loginBTN_Click(sender As Object, e As EventArgs) Handles loginBTN.Click

    Dim cnn2 As New OleDbConnection
    Dim Command As OleDbCommand
    Dim i As Integer
    Dim sql2 As String
    Dim status As String

    Try
        cnn2 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Renz\Documents\Visual Studio 2012\FINAL\Database\AuditDB.mdb;")
        cnn2.Open()

        sql2 = "INSERT INTO Audit ([ID],[TIMEIN]) VALUES('" & frmMain.txtID.Text & "','" & DateTime.Now & "')"

        Command = New OleDbCommand(sql2, cnn2)
        i = Command.ExecuteNonQuery

    Catch ex As Exception
        cnn2.Close()

    End Try

    MessageBox.Show("You have successfully Signed in." & vbNewLine & " Please Don't Forget to Logout.")
    frmMain.cnn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Renz\Documents\Visual Studio 2012\FINAL\Database\UsersDB.accdb;")
    frmMain.cnn.Open()
    frmMain.adptr = New OleDbDataAdapter(frmMain.sql, frmMain.cnn)
    frmMain.adptr.Fill(frmMain.ds)
    If frmMain.ds.Tables(0).Rows(i).Item(0) = lblSI.Text Then
        frmMain.sql = "UPDATE TblUser SET LOGINSTATUS = 'SignedIn'"

        frmMain.cnn.Close()
    End If
    Me.Close()
    frmMain.Show()
    frmMain.txtID.Text = String.Empty
    frmMain.txtPassword.Text = String.Empty
End Sub

我无法让我的 SELECT 和 UPDATE sql 语句工作,该语句将从表单 1 中获取数据集。当单击按钮时,它会将字段“LoginStatus”更新为“Signed In”的值。

PS我正在创建一个超时监控系统,它将确定用户是管理员还是学生,并将他们重定向到相应的表单。学生表格将有登录和退出按钮,并将记录他们的进出时间。单击登录时,它将显示为灰色。

请帮忙..

4

2 回答 2

0

我认为您缺少第二种形式的 executeNonQuery 并且只有一个用户,如果有多个用户考虑使用“Where Clause”,则查询就可以了。

sql= "UPDATE TblUser SET LOGINSTATUS = 'SignedIn' where ID =" <provide id>

如果这不能解决您的问题,请随时提出更多问题。

问候。

于 2013-10-07T11:03:54.267 回答
0
If frmMain.ds.Tables(0).Rows(i).Item(0) = lblSI.Text Then
    frmMain.sql = "UPDATE TblUser SET LOGINSTATUS = 'SignedIn'"

    frmMain.cnn.Close()
End If

似乎 UPDATE 语句没有被执行。我没有看到 ExecuteNonQuery 。

ps: UPDATE语句是更新表中的所有记录。小心!
ps2:我不熟悉 VB(.net) :)

于 2013-10-07T06:14:40.563 回答