Question

. I would really appreciate if somebody could help me out to fix this problem

I'm developing an application using npgsql and c# and when i want to retrieve some information from a table which contains a column with an array of elements, the problem starts when I want to use a query like this:

   NpgsqlCommand cmd = new NpgsqlCommand("select * from book_editions", conn);

the compiler shows the column which contains the elements example({1st,2nd,3d}) as "Data.System.String[] therefore i tried to fix the problem by using parameters.... like is shown here below.

    NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=postgres; " +"Password=admin;Database=library_system;");
    conn.Open();

    //////string[] options = new string[] { "marketing", "m" }; already tried and failed

    ArrayList l = new ArrayList();
        l.Add("2th");
        l.Add("3th");

        NpgsqlParameter p = new NpgsqlParameter("parameterlist", NpgsqlTypes.NpgsqlDbType.Array | NpgsqlTypes.NpgsqlDbType.Text);

        NpgsqlCommand cmd = new NpgsqlCommand("select * from book_editions where editions = any (:parameterlist)", conn);

        //editions is the column which has the array of elements, its datatype is text

        p.Value = l.ToArray();
        cmd.Parameters.Add(p);

     NpgsqlDataReader dr = cmd.ExecuteReader();

     while (dr.Read())

     Console.Write("{0} \n", dr[0].ToString());

     Console.ReadKey();

      conn.Close();

when i execute the code the cmd.ExecuteReader throws this error:

npgsql was unhandled
ERROR: 42883: el operador no existe: text[] = text

i got the following sentence as a tip....

Ningún operador coincide con el nombre y el tipo de los argumentos. Puede ser necesario agregar conversiones explícitas de tipos. (there is no operator which matches with the name and argument types, it would be required to add explicit type castings

does anybody know the solution? :( :( i already tried hard.

Was it helpful?

Solution

Updated:

   var cmd = new NpgsqlCommand("select array_to_string(column, ', '),othercolumn,othercolumn from table",connection);

   var l = new ArrayList();
   l.Add("2th");
   l.Add("3th");

   cmd.Parameters.Add(new NpgsqlParameter("parameterlist", NpgsqlDbType.Array | NpgsqlDbType.Varchar));      

   cmd.Parameters[0].Value = l.ToArray();

   var dr = cmd.ExecuteReader();

   while (dr.Read())
     Console.Write("{0} \n", dr[0].ToString());

   Console.ReadKey();
   conn.Close();

Try again!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top