简体   繁体   中英

How would I go about creating a login form using a database for a check?

I have a DB with a table called person with two rows called personID and firstName

Now, I have a form with 2 text boxes, asking for an ID and a first name.

Thing is, I have no idea how to go about checking the DB if the ID and the firstName are from the same record and taking you to another form called admin if the records match and if they don't just giving an error label.

I would love any help with this, thank you.

EDIT: This is the code as of now, getting the connection already open, connection must be open error

private void btnLoginScreen_Click(object sender, EventArgs e)
{
    {
        switch (dbAuth(txtAdminLogID.Text, txtAdminLogName.Text))
        {
            case true:
                //Open Admin form
                break;
            case false:
                //Show Errorlabel here
                break;
            case null:
                //An error occured while fetching data
                break;
            default:
                break;
        }
    }
}

public bool? dbAuth(string personID, string firstName)
{
    try
    {
        MySqlCommand command = new MySqlCommand();
        command.CommandText = "SELECT * FROM person";
        MySqlDataReader Reader;
        conn.Open();
        Reader = command.ExecuteReader();
        while (Reader.Read())
        {
            if (Reader[0].ToString() == personID) //first check if the ID is equal
            {
                if (Reader[1].ToString() == firstName) //if ID is equal, check if firstName is equal
                {
                    conn.Close();
                    return true;
                }
            }
        }
        conn.Close();
        return false;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        return null;
    }
}

EDIT: This is my connection string:

string connstring = "server=localhost;user Id=root;database=collegesystem;Convert Zero Datetime=True ";
        MySqlConnection conn;

That's how I would go(using MySQL-Connector):

public bool? Authenticate(string personID, string firstName)
{
    try
    {
        MySqlCommand command = connection.CreateCommand();
        command.CommandText = "SELECT * FROM yourTable";
        MySqlDataReader Reader;
        connection.Open();
        Reader = command.ExecuteReader();
        while (Reader.Read())
        {
            if (Reader[0].ToString() == personID) //first check if the ID is equal
            {
                if (Reader[1].ToString() == firstName) //if ID is equal, check if firstName is equal
                {
                    connection.Close();
                    return true; 
                }
            }
        }
        connection.Close();
        return false;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        return null;
    }
}

In your form, where the login button is based use following code:

private void MySQL_Auth_button_Click(object sender, EventArgs e)
        {
        switch (dbAuth.AuthenticatePlain(personID_textBox.Text, firstName_textBox.Text))
                {
                    case true:
                        //Open Admin form
                        break;
                    case false:
                        //Show Errorlabel here
                        break;
                    case null:
                        //An error occured while fetching data
                        break;
                    default:
                        break;
                }
         }

'This is the code you you have to write behind the Login button

SqlConnection CN = new SqlConnection(ConnectionString);

            string query= "Select * from tblUsers Where (user_id=@id AND user_password=@pwd)";

            CN.Open();
            SqlCommand myCommand = new SqlCommand(txt, CN);
            myCommand.Parameters.Add(new SqlParameter("id", SqlDbType.NVarChar)).Value = this.txtUserID.Text;
            myCommand.Parameters.Add(new SqlParameter("pwd", SqlDbType.NVarChar)).Value = this.txtPassword.Text;
            SqlDataReader myReader;
            myReader = myCommand.ExecuteReader();
            myReader.Read();
            if (myReader.HasRows)
            {

                CN.Close();
                AdminForm mf = new AdminForm();
                mf.Show();
                this.Hide();
            }
            else
            {

                MessageBox.Show("Invalid User Name or Password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM