SELECT UPDATE SQL不起作用

时间:2013-10-07 05:52:18

标签: sql vb.net ms-access sql-update

我有2个表格

First Form有

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”值。

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

请帮助..

2 个答案:

答案 0 :(得分: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):)

答案 1 :(得分:0)

我认为你缺少第二种形式的executeNonQuery,只有一个用户然后查询是好的,如果不止一个人考虑使用“Where Clause”,例如。

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

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

问候。