I have a SQL Server table with
CREATE TABLE date_table
(
id int NOT NULL,
my_date date NULL
);
And I'm fetching data into the table from a DataTable in C# with a standard call to Fill like so:
String sql = "SELECT * FROM date_table";
DataTable dt = new DataTable();
SqlCommand command = new SqlCommand(sql, connection);
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(dt);
I expected with the latest versions of C#/SQL Server that the column type in the DataTable for my_date would be DateOnly rather than DateTime. Shouldn't this work transparently now?
I understand how to parse this into a DateOnly, that's not the solution I'm looking for, I'm looking to get a DateOnly in the DataTable.
DateOnlyis a relatively recent addition, andDbDataAdapter/DataTableis essentially a legacy API; personally, I wouldn't expect any adapter implementations to be updated to support it - any reason you're usingDataTable? In most circumstances, anything else is preferableSqlDataReader.GetValuesunder the hood, see github.com/dotnet/runtime/blob/…, which means it's never going to work github.com/dotnet/SqlClient/issues/1009#issuecomment-1257094247 You'd need to loop the reader and add the rows yourself. Have you considered using a proper object model to represent your table, rather than the old styleDataTable?