所以基本上管理員和用戶進入不同的窗口,這裏是代碼C#如何在不同的登錄爲管理員和用戶
private void cmdEnter_Click(object sender, EventArgs e)
{
if (txtUsername.Text == "" && txtPassword.Text == "") //Error when all text box are not fill
{
MessageBox.Show("Unable to fill Username and Password", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (txtUsername.Text == "") //Error when all text box are not fill
{
MessageBox.Show("Unable to fill Username", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (txtPassword.Text == "") //Error when all text box are not fill
{
MessageBox.Show("Unable to fill Password", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
try
{
string myConnection = "datasource=localhost;port=3306;username=root";
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlCommand SelectCommand = new MySqlCommand("select * from boardinghousedb.employee_table where username='" + this.txtUsername.Text + "' and password='" + this.txtPassword.Text + "' ;", myConn);
MySqlDataReader myReader;
myConn.Open();
myReader = SelectCommand.ExecuteReader();
int count = 0;
while (myReader.Read())
{
count = count + 1;
}
if (count == 1)
{
MessageBox.Show("Username and Password . . . is Correct", "Confirmation Message", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.Hide();
Menu mm = new Menu();
mm.ShowDialog();
}
else if (count > 1)
{
MessageBox.Show("Duplicate Username and Password . . . Access Denied", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("Username and Password is Not Correct . . . Please try again", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error);
myConn.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
,但我不知道我怎麼了,其他教程關於本地數據庫會談,但很使用MySQL Here is the employee table, title=admin or user
對於新手來說,這個代碼是SQL注入敞開的。您將要查看參數化查詢。除此之外,我不清楚你在問什麼。這段代碼不工作的方式是什麼?有什麼問題? – David
似乎我可以登錄*您的任何*帳戶,只要我的密碼是'PLAIN WRONG'UNION SELECT * from boardinghousedb.employee_table LIMIT 1; - ' – nvoigt
您檢查'if count == 1'是否正確..如果您正在檢查重複項,這仍然是不正確的..您需要構建您的數據庫,以便您擁有用戶和管理員以及結構的Id你的查詢返回那個..然後在你的查詢中,如果用戶名和密碼是正確的,那麼檢查他們是否嘗試訪問管理員,當他們應該是一個用戶時,然後顯示一條消息並學習如何在檢查後使用'return'關鍵字消息..如果你有空白的用戶和密碼,你需要顯示消息,並立即'返回'意味着退出該方法.. – MethodMan