2016-04-23 90 views
0

我不知道什麼是真正的問題,因爲沒有錯誤報告。所以我想要這些代碼要做的是將事務記錄插入到數據庫中,但沒有任何內容被返回。下面是與此相關的代碼:mysql,vb.net - 保存交易不起作用

的MainForm

Private Sub PayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayButton.Click 
     Dim payment As New Payment 
     payment.Show() 
     AddHandler payment.PaymentEvent, AddressOf paymentSuccess 
     payment.PaymentAmount = TransactionTotal 
    End Sub 

Public Sub paymentSuccess(ByVal sender As Object, ByVal e As Payment.PaymentMadeEventArgs) 
      mydbcon = New MySqlConnection 
      mydbcon.ConnectionString = "server=localhost;userid=root;password=;database=sdudb" 
      Dim reader As MySqlDataReader 
      Try 
       mydbcon.Open() 
       Dim Query As String 
       Query = "select * from inventory" 
       COMMAND = New MySqlCommand(Query, mydbcon) 
       reader = COMMAND.ExecuteReader() 
       While reader.Read 
        Dim itId As Integer = reader.GetString("itemid") 
        Dim itName As String = reader.GetString("itemname") 
        If e.PaymentSuccess = True Then 
         paymentSuccessQuery(itId, itName) 
        End If 
       End While 
       reader.Close() 
       mydbcon.Close() 
      Catch ex As Exception 
       MessageBox.Show(ex.Message) 
      End Try 
     End Sub 
Private Sub paymentSuccessQuery(ByVal itemid, ByVal itemname) 
     mydbcon = New MySqlConnection 
     mydbcon.ConnectionString = "server=localhost;userid=root;password=;database=sdudb" 
     Dim reader As MySqlDataReader 
     Try 
      mydbcon.Open() 
      Dim Query As String 
      Query = "INSERT INTO transaction (itemid, itemname) VALUES('" & itemid & "', '" & itemname & "')" 
      COMMAND = New MySqlCommand(Query, mydbcon) 
      reader = COMMAND.ExecuteReader() 
      If reader.Read Then 
       MessageBox.Show("Unable to save transaction!") 
      Else 
       MessageBox.Show("Transaction Saved!") 
      End If 
      reader.Close() 
      mydbcon.Close() 
     Catch ex As Exception 
      MessageBox.Show(ex.Message) 
     End Try 
    End Sub 

Transactionform

Public Class Payment 
    Public Delegate Sub PaymentMadeEvent(ByVal sender As Object, ByVal e As PaymentMadeEventArgs) 
    Public Event PaymentEvent As PaymentMadeEvent 

    Private _paymentAmount As Decimal 
    Public Property PaymentAmount As Decimal 
     Get 
      Return _paymentAmount 
     End Get 
     Set(ByVal value As Decimal) 
      _paymentAmount = value 
      AmountBox.Text = String.Format("{0:c}", _paymentAmount) 
     End Set 
    End Property 

    Private Sub PayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayButton.Click 
     Dim total As Decimal = 0 

     Try 
      total = Decimal.Parse(AmountBox.Text.TrimStart("₱")) - Decimal.Parse(PaymentBox.Text) 
     Catch 
      MessageBox.Show("Error Occured, please enter a valid amount!") 
      Return 
     End Try 

     If (total > 0) Then 
      AmountBox.Text = total.ToString() 
     Else 
      MessageBox.Show("Please give " + String.Format("{0:c}", -total)) 
      RaiseEvent PaymentEvent(Me, New PaymentMadeEventArgs() With {.PaymentSuccess = True}) 
     End If 

    End Sub 

    Public Class PaymentMadeEventArgs 
     Inherits EventArgs 
     Private _paymentSuccess As Boolean 
     Public Property PaymentSuccess As Boolean 
      Get 
       Return _paymentSuccess 
      End Get 
      Set(ByVal value As Boolean) 
       _paymentSuccess = value 
      End Set 
     End Property 
    End Class 
End Class 

回答

0

ExecuteReader執行命令(插入),但它已建成返回SELECT命令提取的行。
調用Read來發現您的INSERT是否成功在這種情況下是沒有意義的。

你應該調用ExecuteNonQuery,捕獲返回值,如果它不等於零,那麼你已經插入了記錄。

Private Sub paymentSuccessQuery(ByVal itemid, ByVal itemname) 
    Using mydbcon = New MySqlConnection("server=localhost;userid=root;password=;database=sdudb" 
    Try 
     mydbcon.Open() 
     Dim Query As String 
     Query = "INSERT INTO transaction (itemid, itemname) " & _ 
       "VALUES(@id, @name)" 
     Using COMMAND = New MySqlCommand(Query, mydbcon) 
      COMMAND.Parameters.Add("@id", MySqlDbType.VarChar).Value = itemid 
      COMMAND.Parameters.Add("@name", MySqlDbType.VarChar).Value = itemname 
      Dim rowsAdded = COMMAND.ExecuteNonQuery() 
      if rowsAdded = 0 Then 
       MessageBox.Show("Unable to save transaction!") 
      Else 
       MessageBox.Show("Transaction Saved!") 
      End If 
     End Using 
    Catch ex As Exception 
     MessageBox.Show(ex.Message) 
    End Try 
    End Using 
End Sub 

還要注意,我已經改變你的代碼中使用周圍像極爲重要提示的連接和命令,一次性的對象適當的使用聲明,我已經改變了您的查詢使用更安全的參數化查詢方式(不知道關於ID參數的MySqlDbType,它似乎是一個整數,但在你原來的查詢中,你把它放在單引號之間,比如一個字符串)