MySQL 如何在vb.net中更新mysql数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21469167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:56:15  来源:igfitidea点击:

how to update mysql database in vb.net

mysqlvb.net

提问by Chris Gee

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理Button2.Click

    Dim remarks As String
    Dim am_OUT As String = "11:30:01 AM"


    If LblTime.Text >= am_OUT Then
        remarks = "OVERTIME"
    Else
        remarks = "PRESENT"
    End If


    sql = "UPDATE attendance SET  am_out=@am_out, noon_remarks=@noon_remarks " & _
          "WHERE id_no= '" & Txtid.Text & "'AND date LIKE  '" & LblDate.Text & "'and am_out is null"

    Try
        With com
            .Connection = con
            .CommandText = "SELECT * FROM attendance WHERE id_no LIKE '" & Txtid.Text & "' AND date LIKE '" & LblDate.Text & "'AND am_out is NOT NULL"

        End With



        com.Parameters.Add(New MySqlParameter("@am_out", LblTime.Text))
        com.Parameters.Add(New MySqlParameter("@noon_remarks", remarks))

        com.ExecuteNonQuery()

        If remarks = "OVERTIME" Then
            MessageBox.Show("You have little overtime")
        ElseIf remarks = "PRESENT" Then
            MessageBox.Show("You Log out on time")
        End If




    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

End Class

结束类

i cant update my database table in mysql and mysql database already have amin and am remarks

我无法在 mysql 中更新我的数据库表,而 mysql 数据库已经有 amin 和 am 备注

回答by Steve

I suppose that the object comis you MySqlCommand. If this is the case the line

我想这个对象com是你 MySqlCommand。如果是这种情况,线路

 .CommandText = "SELECT * FROM attendance WHERE id_no LIKE '" & Txtid.Text & "' AND date LIKE '" & LblDate.Text & "'AND am_out is NOT NULL"

is a SELECT statement and cannot update anything when this command is executed

是一个 SELECT 语句,执行此命令时不能更新任何内容

Said that, I suggest to correctly implement a parameterized query for every part of your query, and, of course, use the correct statement that updates your record

说了这么多,我建议你对查询的每一部分都正确实现参数化查询,当然,使用正确的语句来更新你的记录

sql = "UPDATE attendance SET  am_out=@am_out, noon_remarks=@noon_remarks " & _
      "WHERE id_no= @id AND date LIKE  @dt and am_out is null"

Try
    With com
        .Connection = con
        .CommandText = sql
        .Parameters.AddWithValue("@am_out",LblTime.Text)
        .Parameters.AddWithValue("@noon_remarks",remarks)
        .Parameters.AddWithValue("@id",Txtid.Text )
        .Parameters.AddWithValue("@dt",Convert.ToDateTime(LblDate.Text ))
    End With
    com.ExecuteNonQuery()