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積分,但是我怎樣才能檢查並顯示錯誤消息呢?
我建議創建一個存儲過程來執行此操作,返回值指示成功或失敗。檢查可用的信用,並在交易中扣除金額(如果信用額度足夠)。 –
我想你應該首先讀取可用的信用,然後驗證是否足夠,如果不是彈出消息,則請繼續更新! –