小编Tyl*_*ngs的帖子

PostgreSQL,Npgsql 返回 42601:“$1”处或附近的语法错误

我正在尝试使用 Npgsql 和/或 Dapper 来查询表,但我一直遇到 Npgsql.PostgresException 42601: syntax error at or near "$1".

这是我使用 NpgsqlCommand 进行的尝试:

  using (var conn = new NpgsqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["postgres"].ConnectionString))
  {
    conn.Open();
    using (NpgsqlCommand command = new NpgsqlCommand("select * from Logs.Logs where Log_Date > current_date - interval @days day;", conn))
    {
      command.Parameters.AddWithValue("@days", days);
      var reader = command.ExecuteReader();
Run Code Online (Sandbox Code Playgroud)

我也用 Dapper(我的首选方法)尝试过:

  using (var conn = new NpgsqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["postgres"].ConnectionString))
  {
    conn.Open();
    var logs = conn.Query<Log>("select * from Logs.Logs where Log_Date > current_date - interval @days day;", new {days = days});
Run Code Online (Sandbox Code Playgroud)

无论哪种方式,我都会得到相同Npgsql.PostgresException …

postgresql npgsql dapper

5
推荐指数
2
解决办法
1万
查看次数

标签 统计

dapper ×1

npgsql ×1

postgresql ×1