Closed brendonparker closed 2 months ago
Have you checked docs for parameterized statements?
No. But I’ll test an example using that approach to rule out Dapper.
Ok - so my problem was using a named parameter with @
instead of $
. Thanks for turning me on to that.
May consider supporting parameters as @
as since that is more common, I think. (At least my experience).
Thanks for your work on this. It's awesome!
Final working snippet:
using Dapper;
using DuckDB.NET.Data;
await File.WriteAllTextAsync("./items.csv", """
ItemId,TransactionDate,Quantity
1001,2024-04-11T00:00:00,1
1002,2024-04-11T00:00:00,1
1003,2024-04-11T00:00:00,1
1003,2024-04-10T00:00:00,1
""");
using var connection = new DuckDBConnection("DataSource=:memory:");
connection.Open();
await connection.ExecuteAsync("CREATE TABLE items as SELECT * FROM read_csv('./items.csv')");
var items = await connection.QueryAsync<Item>(
sql: "SELECT * FROM items WHERE TransactionDate > $TransactionDate",
param: new { TransactionDate = new DateTime(2024, 04, 10) });
Console.WriteLine($"Loaded {items.AsList().Count} items");
internal class Item
{
public long ItemId { get; set; }
public DateTime TransactionDate { get; set; }
public int Quantity { get; set; }
}
I'm getting the following exception when trying to use Dapper and query using a DateTime parameter:
Here is code to reproduce: