【问题标题】:check entered values exist or not IF yes prevent saving it Else检查输入的值是否存在 IF 是 防止保存 否则
【发布时间】:2017-10-16 09:20:27
【问题描述】:

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

现在我想通过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

【问题讨论】:

    标签: mysql sql vb.net visual-studio


    【解决方案1】:

    我已通过添加此代码解决了我的问题

        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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-28
      • 2010-10-12
      • 1970-01-01
      • 1970-01-01
      • 2016-09-10
      • 2015-06-30
      • 2017-09-25
      • 2020-12-14
      相关资源
      最近更新 更多