繁体   English   中英

VB.NET积分系统

[英]VB.NET Credits System

我有一个Mysql数据库设置:

表帐户:

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积分,但是如何查看并显示错误消息呢?

您可以在程序中添加一个功能来检查是否有足够的积分:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM