【问题标题】:npgsql, arrays and C# the operator does not exist: text[] = textnpgsql、数组和 C# 运算符不存在:text[] = text
【发布时间】:2014-01-17 19:13:37
【问题描述】:

。如果有人能帮我解决这个问题,我将不胜感激

我正在使用 npgsql 和 c# 开发一个应用程序,当我想从包含具有元素数组的列的表中检索一些信息时,当我想使用这样的查询时问题就开始了:

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

编译器将包含元素 example({1st,2nd,3d}) 的列显示为“Data.System.String[] 因此我尝试通过使用参数来解决问题.... 如下所示。

    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();

当我执行代码时,cmd.ExecuteReader 会抛出这个错误:

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

我得到以下句子作为提示......

Ningún 操作员与 con el nombre y el tipo de los argumentos 一致。 Puede ser necesario agregar conversiones explícitas de tipos。 (那里 不是与名称和参数类型匹配的运算符,它 需要添加显式类型转换

有人知道解决办法吗? :( :( 我已经很努力了。

【问题讨论】:

  • 你试过用List吗?
  • 没有,如果能给我一个例子,我将不胜感激,非常感谢!

标签: c# arrays npgsql


【解决方案1】:

更新:

   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();

再试一次!

【讨论】:

  • 非常感谢!我现在正在尝试您的解决方案,编译器现在抛出以下错误: 错误:42704: no se pudo encontrar un tipo de array para el tipo de dato text[](""" it was not possible to find an array type for数据类型 text[])
  • 好的,试试看(尝试:'NpgsqlDbType.VarChar',我不确定,使用你的智能感知,更改 cmd.parameters.... 行:cmd.Parameters。 Add("@parameterlist", NpgsqlDbType.Array | NpgsqlDbType.VarChar).Value = l; 我将编辑我的帖子
  • 再次非常感谢您抽出宝贵的时间 :),不幸的是,它向我显示了与以前相同的错误,但现在数据类型为 varchar 错误:42883:运算符不存在:文本 [] = 字符变化
  • 哦我改了:)因为编译器抛出了语法错误:(
  • 还有其他建议吗? :( :(
猜你喜欢
  • 2016-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多