Help with problem CS1586 visual studia next to MySQL

1

I do not understand why I get an error CS1586, specifically when I say new string[]

class ConsultaMySQL
    {
        private MySqlConnection conexion = new MySqlConnection("server=localhost; database=thepacioli; Uid=root; pwd=Valentin_01; Sslmode=none");
        private DataSet ds;

        private DataTable MostrarDatos()        
        {
          conexion.Open();
            MySqlCommand cmd = new MySqlCommand("select *from usuario", conexion);
            MySqlDataAdapter ad = new MySqlDataAdapter(cmd);
            ds = new DataSet();
            ad.Fill(ds, "tabla");
            conexion.Close();
            return ds.Tables["tabla"];            
        }

        private DataTable Buscar(string nombre)
        {
            conexion.Open();
            MySqlCommand cmd = new MySqlCommand(string.Format("select *from usuario where nombre like '%(0)$'",nombre), conexion);
            MySqlDataAdapter ad = new MySqlDataAdapter(cmd);
            ds = new DataSet();
            ad.Fill(ds, "tabla");
            conexion.Close();
            return ds.Tables["tabla"];
        }

        public bool Insertar(string id, string nombre, string segundo_nombre, string apellido, string sexo)
        {
            conexion.Open();

            MySqlCommand cmd = new MySqlCommand(string.Format("Insert into usuario values (0), '(1)', '(2)', '(3)', '(4)'", new string[](id, nombre, segundo_nombre, apellido, sexo)), conexion);
            int filasafectadas = cmd.ExecuteNonQuery();
            conexion.Clone();

            if (filasafectadas > 0) return true;
            else return false;
        }

        public bool Eliminar(string id)
        {
            conexion.Open();

            MySqlCommand cmd = new MySqlCommand(string.Format("delete from usuario where id={0}", id), conexion);
            int filasafectadas = cmd.ExecuteNonQuery();
            conexion.Clone();

            if (filasafectadas > 0) return true;
            else return false;
        }

        public bool Actualizar(string id, string nombre, string segundo_nombre, string apellido, string sexo)
        {
            conexion.Open();

            MySqlCommand cmd = new MySqlCommand(string.Format("update usuario set nombre={0}, segundo_nombre={1}, apellido={2}, sexo={3} where id={4}", new string[] (nombre, segundo_nombre, apellido, sexo, id)), conexion);
            int filasafectadas = cmd.ExecuteNonQuery();
            conexion.Clone();

            if (filasafectadas > 0) return true;
            else return false;
        }
    }
    
asked by Jatniel Nuñez 03.10.2018 в 04:43
source

1 answer

4

This error is perfectly explained in the Microsoft documentation . You can not initialize an array without specifying its size:

int[] a = new int[];   // CS1586  
// debes usar lo siguiente:
int[] b = new int[5];  

By the way, in the code that samples does not appear new string[] anywhere, and also this error has absolutely nothing to do with MySql.

Edited

Now I see how you are using it. In your code you have this:

new string[](id, nombre, segundo_nombre, apellido, sexo)

You are trying to initialize an array of string inline . To do this, you must use {} instead of () :

new string[] { id, nombre, segundo_nombre, apellido, sexo }
    
answered by 03.10.2018 / 09:31
source