无法更新mySQL数据库中的数据

时间:2016-01-07 14:40:51

标签: mysql vb.net

我想要做的是,首先检查ID号是否存在,然后检查是否存在然后执行更新过程,但问题是,它不会更新。问题是什么 ?

$(document).ready(function() {

    $('#calendar').fullCalendar({
        ...
        minTime: "09:00:00",
        maxTime: "17:00:00",
        ...
    });
});

2 个答案:

答案 0 :(得分:0)

Imports MySql.Data.MySqlClient

Public Class Form1
    Private sqlconn As MySqlConnection
    Private query, query1 As String
    Private cmd As MySqlCommand
    Private reader As MySqlDataReader

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        sqlconn = New MySqlConnection
        sqlconn.ConnectionString = "server=localhost;userid=root;password='';database=innovative"
        Try
            sqlconn.Open()
            query = "SELECT Full_Name FROM employee WHERE ID='" & txt_id_number.Text & "'"
            cmd = New MySqlCommand(query, sqlconn)
            reader = cmd.ExecuteReader
            If reader.HasRows = False Then
                MsgBox("Invalid ID number please secure that the  ID number is already Exist" & vbNewLine & "TAKE NOTE:" & vbNewLine & "You cannot update or change the existing ID number for it is the primary Key for the Employee, If you want to Change it, its better to delete the Employee then add it again." & vbNewLine & "Other than that you can change the Full name, age, contact and etc.", vbCritical)
            Else
              query1 = "UPDATE employee SET Full_Name = @txt_fullname, Employee_Type=txt_employee_type, Age=@txt_age'" 
        cmd = New MySqlCommand(query1, sqlconn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.Add("@txt_fullname", SqlDbType.VarChar, 255).Value = txt_fullname.Text
        cmd.Parameters.Add("@txt_employee_type", SqlDbType.VarChar, 255).Value = txt_employee_type.Text
        cmd.Parameters.Add("@txt_age", SqlDbType.VarChar, 255).Value = txt_age.Text
        cmd.Parameters.Add("")
        cmd.ExecuteNonQuery()
                MsgBox(txt_fullname.Text & " was successfully updated", vbInformation)
                txt_age.Text = ""
                txt_contact.Text = ""
                txt_email.Text = ""
                txt_employee_type.Text = ""
                txt_fullname.Text = ""
                txt_id_number.Text = ""
                txt_status.Text = ""
                cb_sex.Text = ""
                add_employee()
            End If
            sqlconn.Close()
            reader.Close()
        Catch ex As Exception
        Finally
            sqlconn.Dispose()
        End Try
    End Sub
End Class

答案 1 :(得分:0)

要改变的三件事。

  1. 使用cmd。ExecuteNonQuery进行插入或更新查询。
  2. 未关闭conn。Open时请勿再次使用;它返回'连接已经打开'错误和执行将终止以捕获阻止。 这就是您的代码无效的原因。
  3. Parameterize安全和类型转换的查询。
  4. 快乐的编码!