如何在vb中搜索和更新记录mysql

时间:2012-04-18 01:01:36

标签: mysql vb.net

我有搜索和更新记录数据库sql的问题。 这是我的代码。我使用mysql数据库和Microsoft Visual Basic 2008

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

    Dim t As New Threading.Thread(AddressOf closeMsgbox)
    objconn = New MySqlConnection("server=localhost;database=AAA;userid=root;password= 'root'")
    Dim username As Boolean = True
    objconn.Open()
    Dim sqlquery As String = "SELECT * FROM daftar WHERE nid = '" & FormRegister.TextBox1.Text & "';"
    Dim data As MySqlDataReader
    Dim adapter As New MySqlDataAdapter
    Dim command As New MySqlCommand
    command.CommandText = sqlquery
    command.Connection = objconn
    adapter.SelectCommand = command
    data = command.ExecuteReader
    If data.HasRows() = True Then
        While data.Read()
            FormRegister.Show()
            tkhupd = Now.ToString("yyyy-MM-dd HH:mm:ss")
            command.Connection = objconn
            command.CommandText = "UPDATE visitor SET tkhupd  ='" & tkhupd & "' WHERE                      nokp = '" & FormRegister.TextBox1.Text & "';"
            command.ExecuteNonQuery()
            MessageBox.Show("You're has logout")
            FormRegister.TextBox1.Text = ""
            username = False
            Me.Close()
        End While
    Else
        FormRegister.Show()
        username = True
    End If
    data.Close()
    If username = True Then
        Dim sqlquery2 As String = "INSERT INTO visitor (nid)VALUES ('" & FormRegister.TextBox1.Text & "')"
        Dim data2 As MySqlDataReader
        Dim adapter2 As New MySqlDataAdapter
        Dim command2 As New MySqlCommand
        command2.CommandText = sqlquery2
        command2.Connection = objconn
        adapter2.SelectCommand = command2
        data2 = command2.ExecuteReader
        MessageBox.Show("You're has login")
        Form4.Show()
        FormRegister.TextBox1.Text = ""
        Me.Close()
    End If
End Sub

但我在Word command.ExecuteNonQuery()上有错误:MySqlException未处理。已经有一个与此Connection关联的开放DataReader,必须先关闭

2 个答案:

答案 0 :(得分:0)

您需要在MySqlCommand语句中使用单独的command2对象,例如While,因为command已在使用中:

Dim command2 As New MySqlCommand
..
While data.Read()
    ..
    command2.Connection = objconn
    ..
End While

答案 1 :(得分:0)

我会在对数据库的一次调用中完成所有操作,包括两个语句。我更习惯于Sql Server,所以这个语法可能有些偏差,但它看起来像这样:

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

    Dim sqlquery As String = _
         "DECLARE @RowCount Int;" & _
         " UPDATE FROM visitor v" & _
            " INNER JOIN daftar d ON d.nid = v.nokp" & _
            " SET v.tkhupd = current_timestamp" & _
            " WHERE d.nid = ?NID;" & _
         " SELECT row_count() INTO @RowCount;" & _
         " IF @RowCount = 0 THEN " & vbCrLf & _
            " INSERT INTO visitor (nid) VALUES (?NID);" & vbCrLf & _
         " END IF"

    Using conn As New MySqlConnection("server=localhost;database=AAA;userid=root;password= 'root'"), _
          cmd As New MySqlCommand(sqlquery, conn)

        cmd.Parameters.Add("?NID", MySqlDbTypes.Int).Value = Integer.Parse(FormRegister.TextBox1.Text)
        conn.Open()
        cmd.ExecuteNonQuery()
    End Using
End Sub