private void bt_Login_Click(object sender, EventArgs e) { MY_DB dB = new MY_DB(); string str_command; SqlDataAdapter adapter = new SqlDataAdapter(); DataTable table = new DataTable(); if (radioButtonStudent.Checked == true) { str_command = "SELECT * FROM login WHERE username = @User AND password = @Pass"; } else { str_command = "SELECT * FROM users WHERE username = @User AND password = @Pass"; } SqlCommand command = new SqlCommand(str_command, dB.GetConnection); command.Parameters.Add("@User", SqlDbType.VarChar).Value = TextBoxUsername.Text; command.Parameters.Add("@Pass", SqlDbType.VarChar).Value = TextBoxPassword.Text; adapter.SelectCommand = command; adapter.Fill(table); if (table.Rows.Count > 0) { //MessageBox.Show("Ok, next time will be go to Main Menu of App"); this.Hide(); if (radioButtonStudent.Checked == true) { Main_Form main = new Main_Form(); main.ShowDialog(this); this.Close(); } else { HumanResourceForm human = new HumanResourceForm(); int userid = Convert.ToInt32(table.Rows[0][0].ToString()); Globals.setGlobalUserId(userid); human.ShowDialog(this); } } else { MessageBox.Show("Invalid Username Or Password", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
bool check_sys() { // Kiểm tra ID và User đã có trên hệ thống chưa MY_DB db = new MY_DB(); SqlDataAdapter adapter = new SqlDataAdapter(); DataTable table = new DataTable(); SqlCommand commmand = new SqlCommand("SELECT * FROM dbo.users WHERE uid = @Id OR username = @Username", db.GetConnection); commmand.Parameters.Add("@Id", SqlDbType.Int).Value = Convert.ToInt32(textBoxIDUser.Text); commmand.Parameters.Add("@Username", SqlDbType.NChar).Value = textBoxUsername.Text.Trim(); adapter.SelectCommand = commmand; adapter.Fill(table); if (table.Rows.Count > 0)// Đã có id hoặc user trên hệ thống { return(true); } return(false); }