简体   繁体   English

C# - 为什么它给每个人 vip?

[英]C# - Why does it give everyone vip?

I am making a project and i thought i had everything working.我正在做一个项目,我以为我已经一切正常了。 i am trying to make it to where if the user has vip (or if vip is equal to 1) it will open a new form.如果用户有 vip(或者如果 vip 等于 1),我会尝试将它转到哪个位置,它将打开一个新表单。 the problem though is that when one person has vip then everyone does (or if vip is equal to 1 for one person)但问题是,当一个人有 vip 时,每个人都会有(或者如果一个人的 vip 等于 1)

I have tried a lot of things.我已经尝试了很多东西。 and nothing has worked so far.到目前为止没有任何效果。

Here is the button code:这是按钮代码:

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);
    }
}

Here is the User.cs code这是 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;
        }
    }
}

I expected a new form to open ONLY IF the logged in user has vip (and not open it for everyone if one user has vip)我希望只有登录用户拥有 vip 才能打开一个新表单(如果一个用户拥有 vip,则不会为所有人打开它)

The code is doing what you're telling it to do.代码正在做你告诉它做的事情。

In your GetUsers() method you get a list of users.在您的GetUsers()方法中,您可以获得用户列表。 Then you iterate through the items, and if (u.VIP == 1) then you open up a window.然后你遍历这些项目, if (u.VIP == 1)然后你打开一个窗口。 Assuming there are multiple records with VIP == 1 , it would of course open a window multiple times.假设有多个VIP == 1记录,它当然会多次打开一个窗口。

If you want to do that only when current user has VIP permission, you probably want to look up current user credentials in the acquired user list (assuming that it is found there), and if indeed the user has VIP permissions then open the new window.如果您只想在当前用户具有 VIP 权限时才这样做,您可能希望在获取的用户列表中查找当前用户凭据(假设在那里找到),如果用户确实具有 VIP 权限,则打开新窗口.

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

相关问题 C#向Active Directory中的每个人授予权限 - C# Give Permissions to Everyone in Active Directory C#为什么Except和Where Enumerable给出了这个奇怪的结果? - C# Why does Except and Where Enumerable Give This Weird Result? 为什么-2%360在c#中给出-2而不是358# - Why does -2 % 360 give -2 instead of 358 in c# 每个人的C#安装程序应用程序 - c# installer application everyone 为什么ReSharper为C#代码反转IF? 它是否提供更好的性能(甚至略微)? - Why does ReSharper invert IFs for C# code? Does it give better performance (even slightly)? 为什么C#split给我一个以空行结尾的数组? - Why does C# split give me an array ending in an empty line? C# 字符串 - 为什么 null 给我的结果与“”不同? - C# Strings - Why does null give me a different result as opposed to ""? 在SSMS中正常运行时,为什么在c#中调用存储过程会给出InvalidOperationException? - Why does calling Stored Procedure in c# give InvalidOperationException when runs fine in SSMS? 为什么将图像保存在C#位图上的文件大小与原始文件不同? - Why does saving image on C# Bitmap give different file size than original? 当我尝试使用 OpenProjectAsync 打开项目时,为什么 msbuild 会给我一个“不支持语言“C#”错误 - Why does msbuild give me a "language "C#" not supported" error when I try to open a project with OpenProjectAsync
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM