我试图让员工登录,如果他们使用该按钮,则保存用户名和时间,以便在具有不同时间戳的行上多次保存,如下所示:
现在,我想通过在username
和comedata
之间建立关系来阻止用户多次输入数据库,以确保用户只能签署他们的出勤率每天一次。
伪代码:
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
这是我认为需要遵循的逻辑,以防止数据库中存在多个条目。
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
答案 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