2017-10-16 97 views
-3

我試圖讓員工登錄,如果他們使用按鈕,保存用戶名和時間來保存多次不同的時間戳這樣的行:檢查輸入的值是否存在如果是防止保存它其他

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 

回答

0

我已經固定我的問題通過將此代碼

Dim ComeGoCheck As String 
    ComeGoCheck = "SELECT COUNT(*) FROM ComeGo WHERE [email protected] AND [email protected]" 
    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 [email protected] AND [email protected]" 
    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 
相關問題