[英]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.