【问题标题】:button not working when i insert new data插入新数据时按钮不起作用
【发布时间】:2012-02-09 19:34:16
【问题描述】:

当我输入的数据还不可用时。按钮不起作用 但是当我在数据库中输入现有数据时,该按钮用于查找数据库中的现有记录和 msgbox.appear 这是我的编码。 (我使用的是 Microsoft Visual Basic 2008 express edition 数据库 mysql)

Imports MySql.Data.MySqlClient
Public Class Form2
    Public conn As MySqlConnection
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Application.DoEvents()
        Button1.Focus()

        conn = New MySqlConnection
        'conn.ConnectionString = "server=localhost;database=ilmu;userid=root;password= ''"
        Try
            conn.Open()
        Catch ex As Exception
            MessageBox.Show("Error1: " & ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        conn = New MySqlConnection("server=localhost;database=ilmu;userid=root;password= ''")
        Try
            conn.Open()
            Dim sqlquery As String = "SELECT * FROM visitor WHERE nama = '" & TextBox1.Text & "';"
            Dim data As MySqlDataReader
            Dim adapter As New MySqlDataAdapter
            Dim command As New MySqlCommand
            command.CommandText = sqlquery
            command.Connection = conn
            adapter.SelectCommand = command
            data = command.ExecuteReader
            While data.Read()
                If data.HasRows() = True Then
                    If data(2).ToString = TextBox2.Text Then
                        command = New MySqlCommand
                        command.Connection = conn
                        tkhupd = Now.ToString("yyyy-MM-dd HH:mm:tt")
                        command.CommandText = "INSERT INTO visitor(noK,khupd)VALUES ('" & TextBox1.Text & "','" & tkhupd & "')"
                        command.ExecuteNonQuery()
                        MessageBox.Show(" Berjaya, Sila Masuk. ", "Tahniah", MessageBoxButtons.OK, MessageBoxIcon.Information)

                    Else
                        MsgBox("exist")

                    End If
                Else
                    MsgBox("Failed Login.")
                End If
            End While

        Catch ex As Exception

        End Try
    End Sub
End Class

【问题讨论】:

  • 不要使用这样的字符串连接来构建查询字符串。永远!

标签: mysql vb.net


【解决方案1】:

当数据库中没有匹配的记录时,我不确定您要做什么,但是您没有任何代码会在没有匹配条目的情况下被命中。

如果没有匹配的记录,则不满足您的 while 条件,循环中不会发生任何事情。

修复它可能涉及重新排列循环的顺序和 if 条件。

先检查data.hasRows。

例子:

If data.HasRows() = True Then
   While Data.Read
       //code here for found rows
   End While
 Else
    //code for no matching entries
 End If

正如 Joel 的评论中提到的,您确实应该考虑使用参数化查询。

您的插入命令示例已更改:

 command.CommandText = "INSERT INTO visitor(noK,khupd)VALUES (?noK,?khupd)"
 command.Parameters.AddWithValue("?noK",TextBox1.Text)
 command.Parameters.AddWithValue("?khupd", tkhupd)
 command.ExecuteNonQuery()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 2018-09-18
    • 1970-01-01
    相关资源
    最近更新 更多