Bug 43144 - SqlDataReader can't handle fetch of DateTime2(7) columns
Summary: SqlDataReader can't handle fetch of DateTime2(7) columns
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Data (show other bugs)
Version: 4.5.X
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-08-06 15:36 UTC by Tiago Freitas Leal
Modified: 2017-09-01 09:30 UTC (History)
2 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Vendor class (1.46 KB, patch)
2016-08-06 15:36 UTC, Tiago Freitas Leal
Details | Diff

Description Tiago Freitas Leal 2016-08-06 15:36:54 UTC
Created attachment 16928 [details]
Vendor class

I have a table Vendors with columns
Id int,
Name nchar(50),
CreateDate datetime2(7),
ChangeDate datetime2(7),

I have a POCO object with properties
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreateDate { get; set; }
public DateTime ChangeDate { get; set; }

When I fetch a row from the DB, everything runs ok under .NET but on Mono it throws

Unhandled Exception:
System.InvalidCastException: Type is System.String
  at System.Data.SqlClient.SqlDataReader.GetDateTime (Int32 i) <0x3c5c548 + 0x0012b> in <filename unknown>:0
  at TestMono.Console.PocoTest.FetchVendor (Int32 id) <0x3bed190 + 0x001d6> in <filename unknown>:0
  at TestMono.Console.Program.Main () <0x3bb0f58 + 0x000d3> in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Type is System.String
  at System.Data.SqlClient.SqlDataReader.GetDateTime (Int32 i) <0x3c5c548 + 0x0012b> in <filename unknown>:0
  at TestMono.Console.PocoTest.FetchVendor (Int32 id) <0x3bed190 + 0x001d6> in <filename unknown>:0
  at TestMono.Console.Program.Main () <0x3bb0f58 + 0x000d3> in <filename unknown>:0

If I change the table schema and use datetime instead of datetime2(7), everything runs ok on Mono.
Comment 1 Tiago Freitas Leal 2016-08-06 15:48:13 UTC
Tested on Mono 4.4.2.11 (Aug 02 2016)
Comment 2 Tiago Freitas Leal 2016-08-06 15:54:28 UTC
Comment on attachment 16928 [details]
Vendor class

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace TestMono.Console
{
    public class Vendor
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime CreateDate { get; set; }
        public DateTime ChangeDate { get; set; }

        public Vendor FetchVendor(int id)
        {
            using (var cnx = new SqlConnection(ConfigurationManager.ConnectionStrings["TestMono"].ConnectionString))
            {
                using (var cmd = new SqlCommand(string.Empty, cnx))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText =
                        "SELECT Id, Name, CreateDate, ChangeDate FROM Vendors WHERE Id = @Id";
                    cmd.Parameters.AddWithValue("@Id", id).DbType = DbType.Int32;

                    cnx.Open();
                    using (var dr = cmd.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            Id = dr.GetInt32(0);
                            Name = dr.GetString(1);
                            CreateDate = dr.GetDateTime(2);
                            ChangeDate = dr.GetDateTime(3);
                        }
                    }
                    cnx.Close();
                }
            }

            return this;
        }
    }
}

Note You need to log in before you can comment on or make changes to this bug.