2015-04-13 92 views
0

我有一個MySQL數據庫設置:VB.NET積分系統

表覈算:

ID:1
名稱:唐尼
積分:600

我有一個名爲購買 按鈕點擊時運行如下

If credits.Text <= 0 Then 
      MsgBox("Geen credits meer.") 
     Else 

      Dim conn As MySqlConnection 
      conn = New MySqlConnection 
      conn.ConnectionString = "server=xxx;port=3306; user id=xxx; password=xxx; database=xxx" 
      Try 
       conn.Open() 
      Catch myerror As MySqlException 
       MsgBox("error connecting to database") 
      End Try 
      Dim myadapter As New MySqlDataAdapter 

      Dim sqlquery = "UPDATE account SET credits = credits -700 WHERE id='" & Trim(id.Text) & "'" 

      Dim mycommand As New MySqlCommand() 
      mycommand.Connection = conn 
      mycommand.CommandText = sqlquery 
      myadapter.SelectCommand = mycommand 
      mycommand.ExecuteNonQuery() 
      Try 
       conn.Close() 
      Catch myerror As MySqlException 
       MessageBox.Show("Cannot connect to database: " & myerror.Message) 
      Finally 
       conn.Dispose() 
      End Try 

     End If 

例如,我有600學分在我的帳戶。我想購買一個價格爲700美元的產品。該系統說它不可能,因爲你只有600個學分。目前的代碼顯示-100積分,但是我怎樣才能檢查並顯示錯誤消息呢?

+0

我建議創建一個存儲過程來執行此操作,返回值指示成功或失敗。檢查可用的信用,並在交易中扣除金額(如果信用額度足夠)。 –

+0

我想你應該首先讀取可用的信用,然後驗證是否足夠,如果不是彈出消息,則請繼續更新! –

回答

1

您可以只需添加一個功能到您的程序,以檢查是否有足夠的學分:

Public Function checkCredits(ByVal creditsNeeded As Int32, ByVal id As String) As Boolean 
    Dim conn As New MySqlConnection 
    conn.ConnectionString = "server=xxx;port=3306; user id=xxx; password=xxx; database=xxx" 
    conn.Open() 

    Dim mycommand As New MySqlCommand() 
    With mycommand 
     .Connection = conn 
     .CommandType = CommandType.Text 
     .CommandText = "SELECT credits FROM account WHERE id = @id" 
     .Parameters.Add("@id", MySqlDbType.VarChar).Value = id 
    End With 

    Dim creditsAvailable As Int32 = mycommand.ExecuteScalar 
    conn.Close() 

    If creditsAvailable >= creditsNeeded Then 
     Return True 
    Else 
     Return False 
    End If 
End Function 

然後調整上述代碼中使用它:

If credits.Text <= 0 Then 
     MsgBox("Geen credits meer.") 
    Else 
     If checkCredits(700, id.Text) Then 

      Dim conn As MySqlConnection 
      conn = New MySqlConnection 
      conn.ConnectionString = "server=xxx;port=3306; user id=xxx; password=xxx; database=xxx" 
      Try 
       conn.Open() 
      Catch myerror As MySqlException 
       MsgBox("error connecting to database") 
      End Try 
      Dim myadapter As New MySqlDataAdapter 

      Dim sqlquery = "UPDATE account SET credits = credits -700 WHERE id='" & Trim(id.Text) & "'" 

      Dim mycommand As New MySqlCommand() 
      mycommand.Connection = conn 
      mycommand.CommandText = sqlquery 
      myadapter.SelectCommand = mycommand 
      mycommand.ExecuteNonQuery() 
      Try 
       conn.Close() 
      Catch myerror As MySqlException 
       MessageBox.Show("Cannot connect to database: " & myerror.Message) 
      Finally 
       conn.Dispose() 
      End Try 
     Else 
      MessageBox.Show("Not Enough Credits") 
     End If 
    End If