C#-为什么会给每个人VIP?

时间:2019-06-19 22:18:54

标签: c# winforms

我正在做一个项目,我以为我一切正常。我试图使它到达用户具有VIP的位置(或VIP等于1)将打开一个新表格。但是问题是,当一个人拥有VIP时,每个人都会(或者如果一个人的VIP等于1)

我尝试了很多事情。到目前为止没有任何进展。

这是按钮代码:

private void MaterialFlatButton4_Click(object sender, EventArgs e)
{
    List<User> users = User.GetUsers();
    bool NotVip = true;

    foreach (User u in users)
    {
        ListViewItem item = new ListViewItem(new string[] { u.Id.ToString(), u.Username, u.Password, u.VIP.ToString() });
        item.Tag = u;
        if (u.VIP == 1)
        {
            NotVip = false;

            MessageBox.Show("Nothing in here works yet", "<3", MessageBoxButtons.OK);
            this.Hide();
            Form3 f3 = new Form3();
            f3.ShowDialog();
        }
    }
    if (NotVip == true)
    {
        //Failed to login.
        MessageBox.Show("You do not have vip.\n\nPlease purchase and then try again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

这是User.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ch
{
    class User
    {
        //database stuff
        private const String SERVER = "localhost";
        private const String DATABASE = "project";
        private const String UID = "root";
        private const String PASSWORD = "********";
        private static MySqlConnection dbConn;

        // User class stuff
        public int Id { get; private set; }

        public String Username { get; private set; }

        public String Password { get; private set; }

        public int VIP { get; private set; }



        private User(int id, String u, String p, int v)
        {
            Id = id;
            Username = u;
            Password = p;
            VIP = v;
        }

        public static void InitializeDB()
        {
            MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
            builder.Server = SERVER;
            builder.UserID = UID;
            builder.Password = PASSWORD;
            builder.Database = DATABASE;

            string connString = builder.ToString();

            builder = null;

            Console.WriteLine(connString);

            dbConn = new MySqlConnection(connString);

            Application.ApplicationExit += (sender, args) => {
                if (dbConn != null)
                {
                    dbConn.Dispose();
                    dbConn = null;
                }
            };
        }

        public static List<User> GetUsers()
        {
            List<User> users = new List<User>();

            String query = "SELECT * FROM accounts";

            MySqlCommand cmd = new MySqlCommand(query, dbConn);

            dbConn.Open();

            MySqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                int id = (int)reader["id"];
                String username = reader["username"].ToString();
                String password = reader["password"].ToString();
                int v = (int)reader["vip"];


                User u = new User(id, username, password, v);

                users.Add(u);
            }

            reader.Close();

            dbConn.Close();

            return users;
        }
    }
}

我希望只有在已登录用户具有vip的情况下,才能打开一个新表单(如果一个用户具有vip,则不要为所有人打开该表单)

1 个答案:

答案 0 :(得分:0)

代码正在执行您要执行的操作。

在您的GetUsers()方法中,您可以获得用户列表。然后,您遍历所有项目,然后if (u.VIP == 1)打开一个窗口。假设VIP == 1有多个记录,它当然会多次打开一个窗口。

如果仅当当前用户具有VIP权限时才想执行此操作,则可能要在获取的用户列表中查找当前用户凭据(假设在该列表中找到了该用户凭据),并且如果确实该用户具有VIP权限,则打开新窗口。