Ver Mensaje Individual
  #1 (permalink)  
Antiguo 10/04/2016, 13:39
diegoj9999
 
Fecha de Ingreso: noviembre-2015
Ubicación: NULL
Mensajes: 23
Antigüedad: 8 años, 6 meses
Puntos: 1
Problema de Select en C# (MySQL)

Hola estoy tratando de seleccionar el usuario por si ya existe ese usuario no lo inserte, este es el codigo


Código:
 private void button1_Click(object sender, EventArgs e)
        {

            this.iniciarConexion1();

            if (string.IsNullOrEmpty(textBox1.Text) | string.IsNullOrEmpty(textBox2.Text) | string.IsNullOrEmpty(textBox3.Text) | string.IsNullOrEmpty(textBox4.Text))
            {
                MessageBox.Show("Algun campo está vacio");
                return;

            }
            else
            {





                string consulta = "SELECT Usuario FROM usuarios WHERE Usuario='" + textBox1.Text + "'";
                MySqlCommand cmd1 = new MySqlCommand(consulta, connection);
                MySqlDataAdapter adap = new MySqlDataAdapter(cmd1);
                DataTable dt = new DataTable();
                adap.Fill(dt);
                int count = Convert.ToInt32(cmd1.ExecuteScalar());
                connection.Open();

              if(count > 0 )
               MessageBox.Show("Usuario existente");
              return;

                    string cadena = "insert into usuarios(usuario,pass,Mail,pass2) values('" + textBox1.Text + "','" + textBox2.Text + "', '" + textBox3.Text + "','" + textBox4.Text
                                      + "')";
                    MySqlCommand cmd = new MySqlCommand(cadena, connection);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Los datos se guardaron correctamente");
                    connection.Close();
                
               
            }

            
        }
el error es:
Unable to connect to any of the specified MySQL hosts.

Espero me puedan ayudar, gracias