【问题标题】:DataAdapter throws error while filling DataTableDataAdapter 在填充 DataTable 时抛出错误
【发布时间】:2015-09-21 05:11:17
【问题描述】:

我正在尝试使用 NpgsqlDataAdapter 填充 DataTable。我已将我的命令准备为

string commandString=@" drop  table if exists tempdata;  
create temp table tempdata as  SELECT X X X X from (_query_);
SELECT x+x, xx, x-y INTO newTempTable FROM tempdata;

并使用以下函数将数据填充到数据表中

public DataTable Searchpg(string CommandString, NpgsqlParameter[] param)
{
    DataTable ResultTable = new DataTable();
    try
    {

        OpenConnection();

        DbCommandpg.CommandText = CommandString;
        DbCommandpg.Connection = DatabaseConnectionpg;
        DbCommandpg.Parameters.Clear();
        if (param != null)
        {

            DbCommandpg.Parameters.AddRange(param);
        }
        adappg.SelectCommand = DbCommandpg;
        ResultTable.Clear();
        adappg.Fill(ResultTable);
    }
    catch (Exception ex)
    {
        File.writeException(ex.Message, null);
        throw ex;
    }
    finally
    {
        DatabaseConnectionpg.Close();
    }
    return ResultTable;
}

错误发生在adappg.Fill(ResultTable);

错误信息是 {"42P01: 关系 \"tempdata\" 不存在"}

我正在使用 NpgSql 版本 3.0.2.0VS 2013Postgres 9.3

但是当我在 pgadmin 的 sql 编辑器中运行相同的查询时,它运行良好并根据需要返回结果。

更新:查询可以顺利使用 Npgsql 2.0.1.0,但不能使用 3.x

【问题讨论】:

  • 你需要从哪个表创建tmp表?
  • 这样试试drop table if exists tempdata;create temp table tempdata as select p_invno,edate,code,product,quantity,unitprice,trancode from tbl,为什么以前创建Temp Table来填充数据表,但是可以直接用select语句来填充!
  • 我正在尝试将查询返回的结果中的数据插入临时表
  • 显示准确的(从查询返回的结果)查询?

标签: c# sql npgsql dataadapter


【解决方案1】:

这是从 Npgsql 3.x 开始的一个已知问题,在此处描述:https://github.com/npgsql/npgsql/issues/641

简而言之,您不能创建实体(例如表)并在同一个 NpgsqlCommand 中使用该实体 - 只需在单独的命令中发送您的 CREATE TABLE 和您的 SELECT

请参阅上面的问题以获得解释,它与 Npgsql 如何与 PostgreSQL 通信的一些非常低级的细节有关。不幸的是,我们不太可能很快解决这个问题。

【讨论】:

  • 我正在编写代码为CREATE TEMP TABLE TABLENAME AS SELECT Fields From temp Table 我有超过4个这样的命令作为单个查询。我应该先在不同的查询中创建临时表,然后只向该表填充数据吗?
猜你喜欢
  • 2012-06-29
  • 1970-01-01
  • 2023-03-31
  • 2011-03-25
  • 2013-01-31
  • 2013-04-11
  • 2011-08-13
  • 2017-07-26
  • 1970-01-01
相关资源
最近更新 更多