我正在尝试使用 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 …