2012-04-18 68 views
0

我有問題要搜索和更新記錄數據庫sql。 這是我的代碼。我使用MySQL數據庫和Microsoft Visual Basic 2008如何在vb中搜索和更新記錄mysql

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 

,但我有錯誤的字command.ExecuteNonQuery():個MySqlException了未處理。已經有與此連接相關聯的打開的DataReader必須先關閉

+0

你知道如何保存登錄與註銷您的代碼將是容易受到SQL注入 – 2012-04-18 13:38:14

回答

0

你需要使用一個單獨的MySqlCommand對象,說command2While語句中,因爲command已經在積極地利用:

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

在數據庫中使用vb 2008的時間。 – ieyla 2012-04-18 08:45:52

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