0

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;
4

2 回答 2

3

这就是我的方式(使用 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;
    }
}

在您的表单中,登录按钮所在的位置使用以下代码:

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;
                }
         }
于 2013-09-14T14:48:10.237 回答
1

'这是您必须在登录按钮后面编写的代码

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);

            }
于 2013-09-14T14:48:12.527 回答