PostgreSQL:42883运算符不存在:没有时区的时间戳

时间:2017-10-06 07:06:22

标签: c# postgresql

我正在使用C#(Npgsql 3.2.4.1)和PostgreSQL(9.6.5)以及pgAdmin 3。

我的表在PostgreSQL中:

create table Data
(                                        
   idref integer NOT NULL,
   val text,
   dt timestamp without time zone NOT NULL,
   constraint PK_Data_idRef_Dt primary key (idRef, Dt),   
   constraint FK_Data_Ref foreign key(idRef) references Ref(Id)                                    
);

当我运行函数(C#)时:

static void Main(string[] args)
    {
        var connString = "Server = 0.0.0.0; Port = 5432; Userid = user; Password = password; CommandTimeout=300; Timeout = 300; Database = database";
        using (var conn = new NpgsqlConnection(connString))
        {
            NpgsqlCommand cmd = new NpgsqlCommand(@"select Val, Dt from Data where idRef = @Id and Dt >= @Dt and Dt <= @Dt order by Dt asc;", conn);

            NpgsqlParameter parameterId = new NpgsqlParameter("Id", NpgsqlDbType.Integer);
            parameterId.Value = 4;
            cmd.Parameters.Add(parameterId);

            NpgsqlParameter parameterDtBegin = new NpgsqlParameter("DtBegin", NpgsqlDbType.Timestamp);
            parameterDtBegin.Value = new DateTime(2017, 9, 1, 0,0,0);
            cmd.Parameters.Add(parameterDtBegin);

            NpgsqlParameter parameterDtEnd = new NpgsqlParameter("DtEnd", NpgsqlDbType.Timestamp);
            parameterDtEnd.Value = new DateTime(2017, 10, 1, 0, 0, 0);
            cmd.Parameters.Add(parameterDtEnd);

            conn.Open();

            using (var reader = cmd.ExecuteReader())  
                  while (reader.Read())
                    Console.WriteLine(reader.GetString(0));
        }
        Console.ReadKey();
    }

我收到以下错误:{42883: operator does not exist: @ timestamp without time zone} System.Exception {Npgsql.PostgresException}。我看过文章(link),但这个想法对我有用。 谢谢你的回答!

1 个答案:

答案 0 :(得分:1)

它帮助了我:

NpgsqlCommand cmd = new NpgsqlCommand(@"select Val, Dt from Data where idRef = :id and Dt >= :date and Dt <= :date2 order by Dt asc;", conn);

cmd.Parameters.Add(":id", NpgsqlDbType.Integer).Value = 3;
cmd.Parameters.Add(":date", NpgsqlDbType.Timestamp).Value = new DateTime(2017, 9, 1, 0, 0, 0);
cmd.Parameters.Add(":date2", NpgsqlDbType.Timestamp).Value = new DateTime(2017, 10, 7, 0, 0, 0);

conn.Open();

using (var reader = cmd.ExecuteReader())
    while (reader.Read())
        Console.WriteLine(reader.GetString(0));