Question

So I have a table with the following columns: ID, name, adress, etc..

I have been doing some research but I cannot come across the right keywords to find out to do what I want. I would like to be able to take the name value (Which would be say... "John Doe" which is in the database already for sure..) and retrieve the ID of it (from the int MySQL value ID).

I have come across the following code but I cannot seem to figure out how to extend its limits to match my needs.

  connection2.Open();
        cmd.ExecuteNonQuery();
        try
        {
            MySqlDataReader myReader = cmd.ExecuteReader();
            while (myReader.Read())
            {
                Console.WriteLine(myReader.GetString(myReader.GetOrdinal("id")));
            }
            myReader.Close();
        }
        finally
        {
            connection2.Close();
        } 

This is also what I have come up with to the best of my abilities.

        MySqlConnection connection2 = new MySqlConnection("Server=" + server + ";" + "Port=" + port + ";" + "Database=" + database + ";" + "Uid=" + uid + ";" + "Password=" + password + ";");
        string query = @"SELECT id FROM caregiverdatabse WHERE name Like '%" + caregiverNameDisp.Text + "%'";
        MySqlCommand cmd = new MySqlCommand(query, connection2);
Was it helpful?

Solution

You should replace the hard coded parameters with sql parameters, but here is a general idea of what you'll need to do here. Using your present sql query.

    MySqlConnection sqlConn = new MySqlConnection();
    MySqlCommand sqlCmd = new MySqlCommand();
    string sSql = "SELECT id FROM caregiverdatabse WHERE name Like '%" + caregiverNameDisp.Text + "%'";
    sqlConn.ConnectionString = "Server=" + server + ";" + "Port=" + port + ";" + "Database=" + database + ";" + "Uid=" + uid + ";" + "Password=" + password + ";";
    sqlCmd.CommandText = sSql;
    sqlCmd.CommandType = CommandType.Text;
    sqlConn.Open();
    sqlCmd.Connection = sqlConn;
    MySqlDataReader reader = sqlCmd.ExecuteReader();
    List<string> results = new List<string>();

    while (reader.Read())
    {

        results.Add((reader["id"].ToString());

    }

    reader.Close();
    sqlConn.Close();

Keep in mind you can add the reader results to a string, to a list like above, whatever you want to do with it.

OTHER TIPS

this how id en name pawe

        clsMySQL.sql_con.Open();
        string id = ID;
       
        sql = "SELECT *FROM test";
        cmd = new MySqlCommand(sql, clsMySQL.sql_con);
        
        
        sql_cmd = new MySqlCommand(sql, clsMySQL.sql_con);
        MySqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            id = dr["id"].ToString();
            user = dr["user_name"].ToString();
            pass = dr["password"].ToString();
             
            if (name.Text == user && passw.Text == pass)
            {
                string depart = id;
                Hide();

                MessageBox.Show("it works");
                // Then show the main form
                cracker form = new cracker(name.Text);
                form.sid = depart;
                form.Show();
                MessageBox.Show(ID);
            }
        }
        else
        {
            MessageBox.Show("Invalid Login please check username and password");
        }
        clsMySQL.sql_con.Close();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top