[英]If-Statement for the Login Button
我是Visual Studio 2010的新手。我正在創建一個登錄表單,在登錄表單中有一個組合框和一個文本框。 組合框中的項目是員工的職位列表。 每當用戶單擊登錄按鈕時,登錄按鈕中應該有一個if語句,以便在員工的特定位置打開表單。 請幫忙。
這是截圖:
這是代碼:
private void loginbutton_Click(object sender, EventArgs e)
{
string MyConString = "SERVER=localhost;" + "DATABASE=timekeeping;" + "UID=root;" + "PASSWORD=admin;";
MySqlConnection connection = new MySqlConnection(MyConString);
MySqlCommand command = connection.CreateCommand();
MySqlDataReader Reader;
command.CommandText = "select username, password from users";
connection.Open();
Reader = command.ExecuteReader();
while (Reader.Read())
{
if (username_login.Text == Reader[0].ToString() && password_login.Text == Reader[1].ToString().Trim())
{
username = Reader[0].ToString();
password = Reader[1].ToString();
}
}
if (username_login.Text == username && password_login.Text == password.Trim())
{
this.Hide();
Home form = new Home();
//form.userSession(lname, fname);
form.Show();
}
else MessageBox.Show("Invalid User", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
connection.Close();
}
我還沒有包括組合框,因為我不知道該放什么。
我想你需要重新考慮你的設計:
如果將用戶的角色(位置)存儲在/ a數據庫中會更好。
如果有人正確識別自己加載該用戶的角色,然后打開正確的表單/應用程序。 不要讓用戶選擇他的角色。
看看它們不僅僅適用於ASP.NET 的成員資格和角色提供程序 ,您也可以在Winforms中使用它們:
優秀的教程:
如下所示:
Form mainForm;
switch (comboBox1.SelectedText)
{
case "Individual employees":
mainForm = new EmployeesForm();
break;
case "HR":
mainForm = new HRForm();
break;
case "Manager":
mainForm = new ManagerForm();
break;
case "Supervisor":
mainForm = new SupervisorForm();
break;
default:
mainForm = new DefaultForm();
break;
}
mainForm.Show();
這使用一個名為switch的條件語句,它通過將控制權傳遞給其主體中的一個case語句來處理多個選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.