使用单击按钮更新记录

时间:2017-09-29 13:15:07

标签: sql vb.net visual-studio

嗨,大家好我需要一些帮助我是编程的新手。我试图从我的应用程序中的计时器输入一个退出时间到现有时间应用记录的列中。我试图将参数用于我的SQL命令,下面列出了两种不同的方法。任何人都可以给我一些指示。

当我点击似乎来自时间的提交按钮时,我也收到语法错误。

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Label8_Time.Text = Date.Now.ToString("MM-dd-yyy hh:mm")
End Sub

Private Sub Button3_SignOut_Click(sender As Object, e As EventArgs) Handles Button3_SignOut.Click
    Dim ask As MsgBoxResult
    ask = MsgBox("Are You Sure You Would Like To Sign Out", MsgBoxStyle.YesNo, " Message Details")
    If ask = (MsgBoxResult.Yes) Then
        connect.Open()
        Dim command As New SqlCommand() With {
                    .Connection = connect
                }
                             '/ Current Parameter Set I tried 
        command.Parameters.Add("@FIRSTNAME", SqlDbType.VarChar).Value = TextBox1_FirstName.Text
        command.Parameters.Add("@LAST", SqlDbType.VarChar).Value = TextBox2_LastName.Text
        command.Parameters.Add("@EMPID", SqlDbType.Int).Value = TextBox3_EmployeeID.Text   'Conversion in Question
        command.Parameters.Add("@RECID", SqlDbType.Int).Value = TextBox4_RecordID.Text 'Conversion in Question
        command.Parameters.Add("@SIGNTIME", SqlDbType.DateTime).Value = Now.ToString

                           '/ 1st Parameter Set I tried     
        'command.Parameters.Add("@FIRSTNAME", TextBox1_FirstName.Text, DbType.String)
        'command.Parameters.Add("@LAST", TextBox2_LastName.Text, DbType.String)
        'command.Parameters.Add("@EMPID", TextBox3_EmployeeID.Text, DbType.Int32)   'Conversion in Question
        'command.Parameters.Add("@RECID", TextBox4_RecordID.Text, DbType.Int32)   'Conversion in Question
        'command.Parameters.Add("SIGNOUT", Now.ToString, DbType.DateTime)

        'command = New SqlCommand("UPDATE EMPLOYEELOG SET OUTTIME = @SIGNOUT WHERE FIRST= @FIRSTNAME and LAST = @LASTNAME and EMPLOYEEID=@EMPID and IDNO=@RECID", connect)

        command.CommandText = $"Update EmployeeLog  set  OUTTIME = {Now.ToString} WHERE FIRST={TextBox1_FirstName.Text}and LAST={TextBox2_LastName.Text} And EMPID={TextBox3_EmployeeID.Text} and RECID={TextBox4_RecordID}"
        command.ExecuteNonQuery()

2 个答案:

答案 0 :(得分:0)

这应该有用,也会有任何例外。

Private Sub Button3_SignOut_Click(sender As Object, e As EventArgs) Handles Button3_SignOut.Click
    Dim ask As MsgBoxResult
    ask = MsgBox("Are You Sure You Would Like To Sign Out", MsgBoxStyle.YesNo, " Message Details")
    If ask = (MsgBoxResult.Yes) Then
        Try
            connect.Open()
            Using command As New SqlCommand("UPDATE EMPLOYEELOG SET OUTTIME = @SIGNOUT WHERE FIRST= @FIRSTNAME and LAST = @LASTNAME and EMPLOYEEID=@EMPID and IDNO=@RECID", connect)
                command.Parameters.Add("@FIRSTNAME", SqlDbType.VarChar).Value = TextBox1_FirstName.Text
                command.Parameters.Add("@LAST", SqlDbType.VarChar).Value = TextBox2_LastName.Text
                command.Parameters.Add("@EMPID", SqlDbType.Int).Value = CInt(TextBox3_EmployeeID.Text)
                command.Parameters.Add("@RECID", SqlDbType.Int).Value = CInt(TextBox4_RecordID.Text)
                command.Parameters.Add("@SIGNOUT", SqlDbType.DateTime).Value = Now

                command.ExecuteNonQuery()
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

答案 1 :(得分:0)

您是否真的需要Where子句中的所有内容来唯一标识要更新的记录?这只是猜测,但IDNO是Employee日志的PK,所以这就是Where子句中的必要条件。额外的东西要求麻烦。例如,威廉可以签出Will,Willy,Bill,Billy甚至William。你永远不会得到一场比赛! EMPLOYEEID是employees表中的FK,因此名字和姓氏甚至不应该在日志表中。如果不是这样,请忽略。我知道这只是一个评论,很快,也许,我将能够发表评论。 : - )