C # mysql reader.read ()

3

I'm trying to read take the idphone from the phone table, but the reader1 collects the data from this table rarely and almost always he does not.

Corroborating that the data I am looking for exists in said table with the search pattern.

for (int i = 0; i < dataGridView2.Rows.Count - 1; i++)
        {
            Db query = new Db();
            query.Query("INSERT INTO phone(meid,iccid) VALUES('" + dataGridView2.Rows[i].Cells["ICCID"].Value +
            "','" + dataGridView2.Rows[i].Cells["MEID"].Value + "')");

This is the first reader and if it collects the data that I need from the database

            MySqlCommand cmd = new MySqlCommand("SELECT idphone FROM phone_atrribute WHERE sku='" + dataGridView2.Rows[i].Cells["SKU"].Value + "'", GetConexion());
            MySqlDataReader reader = cmd.ExecuteReader();

This is the second and the one that is giving me problems the data is an integer and sometimes picks it up but it is very rare that it happens, I can not see why it works sometimes and sometimes not.

            MySqlCommand cmd1 = new MySqlCommand("SELECT idphone FROM phone WHERE meid='" + dataGridView2.Rows[i].Cells["MEID"].Value + "'", GetConexion1());
            MySqlDataReader reader1 = cmd1.ExecuteReader();

Here I try to insert the data collected by the reader and the reader1 in the database, but since the reader1 does not collect the data this is not executed.

           if (reader.Read() && reader1.Read())
            {
                int phone = reader.GetInt32(0);
                int attribute = reader1.GetInt32(0);

                query.Query("INSERT INTO phone_inventary(phone,phone_attribute) VALUES('"+phone+"','"+attribute+"')");
                reader.Close();
                reader1.Close();

            }


        }
    
asked by user5789 24.04.2016 в 17:14
source

3 answers

1

It seems to me that the problem is that you use two different methods to open the connection, GetConexion() and GetConexion1() , and within the first one you are closing the connection, you can use only one method.

Because of the problem you mention that sometimes if you get data and in others not at this point:

 MySqlCommand cmd1 = new MySqlCommand("SELECT idphone FROM phone WHERE meid='" + dataGridView2.Rows[i].Cells["MEID"].Value + "'", GetConexion1());
            MySqlDataReader reader1 = cmd1.ExecuteReader();

It seems to me that the connection is closing when making a line of code, within GetConexion()

You can use the same connection method for the 2 queries but ensure close the connection until the operations are finished.

 connexionBD.Close();
    
answered by 24.04.2016 в 22:22
0

Now I'm using the same method for both queries but I'm still getting the same problem, change the order of the queries and I checked to see if it was a connection or execution problem but the only query that does not work is.

MySqlCommand cmd1 = new MySqlCommand("SELECT idphone FROM phone WHERE meid='" + dataGridView2.Rows[i].Cells["MEID"].Value + "'", GetConexion1());
        MySqlDataReader reader1 = cmd1.ExecuteReader();

From the first consultation I always get the desired result.

MySqlCommand cmd = new MySqlCommand("SELECT idphone FROM phone_atrribute WHERE sku='" + dataGridView2.Rows[i].Cells["SKU"].Value + "'", GetConexion());
        MySqlDataReader reader = cmd.ExecuteReader();

I still do not know exactly where the error may be because I've checked everything that comes to mind.

    
answered by 25.04.2016 в 00:57
0

If the number is an integer and the field is numeric, you should not use quotation marks in the query:

MySqlCommand cmd1 = new MySqlCommand("SELECT idphone FROM phone WHERE meid=" + dataGridView2.Rows[i].Cells["MEID"].Value, GetConexion1());
MySqlDataReader reader1 = cmd1.ExecuteReader();

And even better if you use parameters:

MySqlCommand cmd1 = new MySqlCommand("SELECT idphone FROM phone WHERE meid=@meid", GetConexion1());
cmd.Parameters.AddWithValue("@meid", dataGridView2.Rows[i].Cells["MEID"].Value);
MySqlDataReader reader1 = cmd1.ExecuteReader();
    
answered by 25.05.2016 в 13:32