检查输入的值是否存在如果是,则阻止保存它

时间:2017-10-16 09:20:28

标签: mysql sql vb.net visual-studio

我试图让员工登录,如果他们使用该按钮,则保存用户名和时间,以便在具有不同时间戳的行上多次保存,如下所示:

Example database

现在,我想通过usernamecomedata 之间建立关系来阻止用户多次输入数据库,以确保用户只能签署他们的出勤率每天一次。

我心中的例子

伪代码:

If username = ComeForm_CGUserName_TextBox.Text
And CGComeDate = ComeForm_CGComeDate_DateTimePicker.Value
And username Is In DB And CGComeDate Is In DataBase
Then
    MsgBox("You have already signed your attendace")
Else
    Insert data into DataBase easy
End If

这是我认为需要遵循的逻辑,以防止数据库中存在多个条目。

那是目前ComeGo表单的完整代码

Public Con As New SqlConnection("Data Source=(localdb)\ProjectsV13;Initial Catalog=Euro_SQL_Server;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False")

Public ComeGoDT As New DataTable
Public ComeGoDA As New SqlDataAdapter
Public MyNewComeGoID As Integer

Public Sub Load_ComeGo()
    ComeGoDT.Clear()
    ComeGoDA = New SqlDataAdapter("select * from ComeGo", Con)
    ComeGoDA.Fill(ComeGoDT)
End Sub

Public Sub Code_ComeGo()
    Dim dt As New DataTable
    Dim da As New SqlDataAdapter("select max(CGID) from ComeGo", Con)
    da.Fill(dt)
    If IsDBNull(dt(0)(0)) = True Then
        MyNewComeGoID = 1
    Else
        MyNewComeGoID = dt(0)(0) + 1
    End If
End Sub

Public Sub NewComeGo()
    Code_ComeGo()
    'Auto Generate EmployeesID
    ComeForm_CGID_TextBox.Text = MyNewComeGoID
    'Clearing Fields
    ComeForm_CGComeDate_DateTimePicker.Value = Now.Date
    ComeForm_CGComeTime_DateTimePicker.Value = Now
    ComeForm_CGUserName_TextBox.Text = MangersMainMenu.MangersMainMenu_CurrentUserResult_Label.Text
    ComeForm_CGNotes_TextBox.Text = ""
    'Auto Generate ActionBy From Logged In UserFullName
    ComeForm_ActionBy_TextBox.Text = MangersMainMenu.MangersMainMenu_CurrentUserResult_Label.Text
End Sub

Private Sub ComeForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Load EditComeGo_Form From EURO_DataBase
    Load_ComeGo()
    'Clear Fields
    NewComeGo()
End Sub

Private Sub ComeForm_ComeSign_Button_Click(sender As Object, e As EventArgs) Handles ComeForm_ComeSign_Button.Click
    'Definition Adding New ComeGo Method
    ComeGoDT.Rows.Add()
    Dim last As Integer = ComeGoDT.Rows.Count - 1
    'Match Each Filed On The DataBase With There Filed On The Table
    ComeGoDT.Rows(last).Item("CGID") = ComeForm_CGID_TextBox.Text
    ComeGoDT.Rows(last).Item("CGDate") = Now.Date
    ComeGoDT.Rows(last).Item("CGTime") = Now
    ComeGoDT.Rows(last).Item("CGUserName") = MangersMainMenu.MangersMainMenu_CurrentUserResult_Label.Text
    ComeGoDT.Rows(last).Item("CGComeDate") = ComeForm_CGComeDate_DateTimePicker.Value
    ComeGoDT.Rows(last).Item("CGComeTime") = ComeForm_CGComeTime_DateTimePicker.Value
    ComeGoDT.Rows(last).Item("CGNotes") = ComeForm_CGNotes_TextBox.Text
    ComeGoDT.Rows(last).Item("ActionBy") = MangersMainMenu.MangersMainMenu_CurrentUserResult_Label.Text
    'Definition Saving New Changes Method
    Dim save As New SqlCommandBuilder(ComeGoDA)
    'Refresh ComeGo DataBase Table
    ComeGoDA.Update(ComeGoDT)
    ComeGoDT.AcceptChanges()
    'Show Massage Box
    MsgBox("تم تسجيل حضور الموظف")
    'Reload ComeGo Table With New UpDates
    Load_ComeGo()
    'Start New ComeGo Entery
    NewComeGo()
End Sub

那是“保存按钮”

Private Sub ComeForm_ComeSign_Button_Click(sender As Object, e As EventArgs) Handles ComeForm_ComeSign_Button.Click
    'Definition Adding New ComeGo Method
    ComeGoDT.Rows.Add()
    Dim last As Integer = ComeGoDT.Rows.Count - 1
    'Match Each Filed On The DataBase With There Filed On The Table
    ComeGoDT.Rows(last).Item("CGID") = ComeForm_CGID_TextBox.Text
    ComeGoDT.Rows(last).Item("CGDate") = Now.Date
    ComeGoDT.Rows(last).Item("CGTime") = Now
    ComeGoDT.Rows(last).Item("CGUserName") = MangersMainMenu.MangersMainMenu_CurrentUserResult_Label.Text
    ComeGoDT.Rows(last).Item("CGComeDate") = ComeForm_CGComeDate_DateTimePicker.Value
    ComeGoDT.Rows(last).Item("CGComeTime") = ComeForm_CGComeTime_DateTimePicker.Value
    ComeGoDT.Rows(last).Item("CGNotes") = ComeForm_CGNotes_TextBox.Text
    ComeGoDT.Rows(last).Item("ActionBy") = MangersMainMenu.MangersMainMenu_CurrentUserResult_Label.Text
    'Definition Saving New Changes Method
    Dim save As New SqlCommandBuilder(ComeGoDA)
    'Refresh ComeGo DataBase Table
    ComeGoDA.Update(ComeGoDT)
    ComeGoDT.AcceptChanges()
    'Show Massage Box
    MsgBox("تم تسجيل حضور الموظف")
    'Reload ComeGo Table With New UpDates
    Load_ComeGo()
    'Start New ComeGo Entery
    NewComeGo()
End Sub

1 个答案:

答案 0 :(得分:0)

我通过添加此代码修复了我的问题

    Dim ComeGoCheck As String
    ComeGoCheck = "SELECT COUNT(*) FROM ComeGo WHERE CGUserName=@CGUserName AND CGComeDate=@CGComeDate"
    Dim cmd As SqlCommand = New SqlCommand(ComeGoCheck, Con)
    Dim query As Integer
    cmd.Parameters.Add("@CGUserName", SqlDbType.VarChar).Value = ComeForm_CGUserName_TextBox.Text
    cmd.Parameters.Add("@CGComeDate", SqlDbType.Date).Value = ComeForm_CGComeDate_DateTimePicker.Value
    Con.Open()
    query = CInt(cmd.ExecuteScalar())
    Con.Close()
    If query > 0 Then
        MsgBox("لقد تم تسجيل حضور الموظف مسبقا")
        Con.Close()
    Else
    End If

所以完整保存按钮代码为:

Private Sub ComeForm_ComeSign_Button_Click(sender As Object, e As EventArgs) Handles ComeForm_ComeSign_Button.Click
    Dim ComeGoCheck As String
    ComeGoCheck = "SELECT COUNT(*) FROM ComeGo WHERE CGUserName=@CGUserName AND CGComeDate=@CGComeDate"
    Dim cmd As SqlCommand = New SqlCommand(ComeGoCheck, Con)
    Dim query As Integer
    cmd.Parameters.Add("@CGUserName", SqlDbType.VarChar).Value = ComeForm_CGUserName_TextBox.Text
    cmd.Parameters.Add("@CGComeDate", SqlDbType.Date).Value = ComeForm_CGComeDate_DateTimePicker.Value
    Con.Open()
    query = CInt(cmd.ExecuteScalar())
    Con.Close()
    If query > 0 Then
        MsgBox("لقد تم تسجيل حضور الموظف مسبقا")
        Con.Close()
    Else
        'Definition Adding New ComeGo Method
        ComeGoDT.Rows.Add()
        Dim last As Integer = ComeGoDT.Rows.Count - 1
        'Match Each Filed On The DataBase With There Filed On The Table
        ComeGoDT.Rows(last).Item("CGID") = ComeForm_CGID_TextBox.Text
        ComeGoDT.Rows(last).Item("CGDate") = Now.Date
        ComeGoDT.Rows(last).Item("CGTime") = Now
        ComeGoDT.Rows(last).Item("CGUserName") = MangersMainMenu.MangersMainMenu_CurrentUserResult_Label.Text
        ComeGoDT.Rows(last).Item("CGComeDate") = ComeForm_CGComeDate_DateTimePicker.Value
        ComeGoDT.Rows(last).Item("CGComeTime") = ComeForm_CGComeTime_DateTimePicker.Value
        ComeGoDT.Rows(last).Item("CGNotes") = ComeForm_CGNotes_TextBox.Text
        ComeGoDT.Rows(last).Item("ActionBy") = MangersMainMenu.MangersMainMenu_CurrentUserResult_Label.Text
        'Definition Saving New Changes Method
        Dim save As New SqlCommandBuilder(ComeGoDA)
        'Refresh ComeGo DataBase Table
        ComeGoDA.Update(ComeGoDT)
        ComeGoDT.AcceptChanges()
        'Show Massage Box
        MsgBox("تم تسجيل حضور الموظف")
        'Reload ComeGo Table With New UpDates
        Load_ComeGo()
        'Start New ComeGo Entery
        NewComeGo()
        Con.Close()
    End If
End Sub