在MySQL中INSERT INTO不工作但没有错误?

时间:2017-02-06 11:18:39

标签: mysql vb.net

我以某种方式无法将数据插入我的MySQL数据库,但我知道查询没有问题,因为没有错误消息,它可以使它成为成功消息框。我认为该查询适用于MySQL,但它不是我应该用于INSERT INTO的特定查询。

这是我的代码:

 Imports MySql.Data.MySqlClient
      Public Class Register
      Dim ServerString As String = "Server=localhost; UserId =root; Password = ; Database = gym;"
      Dim MysqlConn As MySqlConnection = New MySqlConnection
      Dim COMMAND As MySqlCommand
      Dim password, pass As String
      Dim member As Integer

      Private Sub Register_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.CenterToParent()

      End Sub

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        MysqlConn.ConnectionString = ServerString
        Dim READER As MySqlDataReader

        password = TextBox2.Text
        pass = TextBox3.Text

        confirm(password, pass)

        If TextBox1.Text = Nothing Or TextBox2.Text = Nothing Or TextBox3.Text = Nothing Or TextBox4.Text = Nothing Or TextBox5.Text = Nothing Or TextBox6.Text = Nothing Or DateTimePicker1.Text = Nothing Or RadioButton1.Checked = False And RadioButton2.Checked = False Then
            MsgBox("Please Fill your Information Completely")

        Else  
            MysqlConn.ConnectionString = ServerString

              Try
                 MysqlConn.Open()
                 Dim query As String

                 query = "select * from gym.user where user_name ='" & TextBox1.Text & "'"
                 COMMAND = New MySqlCommand(query, MysqlConn)
                 READER = COMMAND.ExecuteReader

                 Dim count As Integer

                 While READER.Read
                    count = count + 1
                 End While

                 MysqlConn.Close()

                 If count > 0 Then
                    MsgBox("User Already Exists")
                 Else     
                    MysqlConn.Open()

                    query = "INSERT INTO gym.user(user_name,user_password,user_firstname,user_lastname,user_birthday,user_contact,user_membership) VALUES ('" & TextBox1.Text & "', md5('" & TextBox2.Text & "') ,'" & TextBox4.Text & "','" & TextBox5.Text & "','" & DateTimePicker1.Value.Date & "','" & TextBox6.Text & "','" & member & "')"
                    COMMAND = New MySqlCommand(query, MysqlConn)


                    MsgBox("USER REGISTERED")
                    MysqlConn.Close()
                 End If


             Catch ex As Exception
                  MsgBox(ex.Message)
             Finally
                  MysqlConn.Dispose()
             End Try

         End If
       End Sub

1 个答案:

答案 0 :(得分:0)

您缺少ExecuteNonQuery声明:

query = "INSERT INTO gym.user(user_name,user_password,user_firstname,user_lastname,user_birthday,user_contact,user_membership) VALUES ('" & TextBox1.Text & "', md5('" & TextBox2.Text & "') ,'" & TextBox4.Text & "','" & TextBox5.Text & "','" & DateTimePicker1.Value.Date & "','" & TextBox6.Text & "','" & member & "')"
COMMAND = New MySqlCommand(query, MysqlConn)

COMMAND.ExecuteNonQuery()