【问题标题】:Why can't I get data from my NPGSQL server?为什么我无法从 NPGSQL 服务器获取数据?
【发布时间】:2019-11-18 19:01:26
【问题描述】:

我需要编写一个 C# 程序,它必须能够管理我服务器上的数据。我有一个带有数据表的 NPGSQL 服务器,我可以向其中写入数据,但是在运行程序时我无法读取数据。我做错了什么?

 public NpgsqlDataReader reader;
    public NpgsqlCommand InsertCommand = new NpgsqlCommand();
    public String sConnectionString;
    public Npgsql.NpgsqlConnection Conn;
    public void DataBaseOpen()
    {
        sConnectionString = "Server=192.168.1.100;Port=5432;Username=postgres;Password=admin;Database=analoginput;Pooling=false;MinPoolSize=1;MaxPoolSize=999;Timeout=15;";

    Conn = new Npgsql.NpgsqlConnection(sConnectionString);

    InsertCommand = Conn.CreateCommand();
    Conn.Open();
    }

  public void DataBaseClose()
        {
        Npgsql.NpgsqlConnection.ClearAllPools();
        Conn.Close();
        }

InsertCommand.CommandText = "Select * From public.sensorlog WHERE \"date\" > '2019.07.08.' And \"date\" < '2019.07.10.' order by Date asc;";
            System.Windows.MessageBox.Show(InsertCommand.CommandText);
            Npgsql.NpgsqlDataReader reader = InsertCommand.ExecuteReader();
            System.Data.DataTable CSV = new System.Data.DataTable();
            while (reader.Read())
            {
                CSV.Load(reader);
            }

我想将数据加载到 CSV 数据表中,但我无法让它工作。数据表只是空的。

【问题讨论】:

  • 您是否尝试将WHERE 替换为LIMIT 以查看日期匹配是否有问题?
  • 我在 pgadmin 查询工具中使用了完全相同的命令,它可以工作
  • 另外,即使我删除了所有限制,并且我的命令是“SELECT * FROM public.sensorlog”,数据表仍然为空
  • 删除CommandText字符串中的;,可能不会影响它 - 但也不需要
  • 还是什么都没有,我就是想不通..在另一个程序中它就像魅力一样工作

标签: c# .net database server npgsql


【解决方案1】:

如果您将代码重构为以下内容会怎样。

using 语句将保证您的连接和命令在超出范围时关闭/处置,并且使用 try/catch 块您将捕获任何异常并通过消息框将其报告给 UI。如果有任何异常,这将有助于捕获异常。

public Npgsql.NpgsqlConnection DatabaseOpen()
{
    var sConnectionString = "Server=192.168.1.100;Port=5432;Username=postgres;Password=xxx;Database=analoginput;Pooling=false;MinPoolSize=1;MaxPoolSize=999;Timeout=15;";

    var Conn = new Npgsql.NpgsqlConnection(sConnectionString);

    Conn.Open();

    return Conn;
}
public void Main()
{
    try
    {
        using (var conn = DatabaseOpen())
        {
            using (var InsertCommand = conn.CreateCommand())
            {
                InsertCommand.CommandText = "Select * From public.sensorlog WHERE \"date\" > '2019.07.08.' And \"date\" < '2019.07.10.' order by Date asc;";
                System.Windows.MessageBox.Show(InsertCommand.CommandText);
                Npgsql.NpgsqlDataReader reader = InsertCommand.ExecuteReader();
                System.Data.DataTable CSV = new System.Data.DataTable();
                while (reader.Read())
                {
                    CSV.Load(reader);
                }
            }
        }
    }
    catch (Exception ex)
    {
        System.Windows.MessageBox.Show(ex.Message);

    }
    finally
    {
        Npgsql.NpgsqlConnection.ClearAllPools();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 2011-10-02
    • 2020-07-17
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多