示例#1
0
        private void Login_Click(object sender, EventArgs e)
        {
            SQLConnect       conn    = new SQLConnect();
            DataTable        table   = new DataTable();
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            MySqlCommand     command = new MySqlCommand();
            String           query   = " SELECT * FROM `appusers` WHERE `username`= @admin AND `password`= @12345;";

            command.CommandText = query;
            command.Connection  = conn.GetConnection();

            command.Parameters.Add("@admin", MySqlDbType.VarChar).Value = username.Text;
            command.Parameters.Add("@12345", MySqlDbType.VarChar).Value = Password.Text;



            adapter.SelectCommand = command;
            adapter.Fill(table);

            if (table.Rows.Count > 0)

            {
                this.Hide();
                Main mform = new Main();
                mform.Show();
            }

            else
            {
                if (username.Text.Trim().Equals("") && Password.Text.Trim().Equals(""))
                {
                    MessageBox.Show("Enter Username and Password to Login", "Empty Username and Password fields", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                else if (username.Text.Trim().Equals(""))
                {
                    MessageBox.Show("Enter Username to Login", "Empty Username field", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                else if (Password.Text.Trim().Equals(""))
                {
                    MessageBox.Show("Enter Password to Login", "Empty Password field", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }



                else
                {
                    MessageBox.Show("Username or Password is not correct. Please try again", "Incorrect username or password", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        public void FillCombo()
        {
            SQLConnect       conn    = new SQLConnect();
            DataTable        dt      = new DataTable();
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            MySqlCommand     command = new MySqlCommand();
            String           query   = " SELECT gender from sex";

            command.CommandText = query;
            command.Connection  = conn.GetConnection();

            adapter.SelectCommand = command;
            adapter.Fill(dt);
            foreach (DataRow dr in dt.Rows)

            {
                sexcombobox.Items.Add(dr["gender"].ToString());
            }
        }