none
Losing milliseconds setting Recordset field to DateTime RRS feed

  • Question

  • I have a C# project and added a reference to the ADO 6.1 dll (so it created an interop assembly for me - Interop.ADOB.6.1 - in my references).

    In my code I create a Recordset with a single datetime field (adDBTimeStamp data type). When I set the field to a DateTime value that contains milliseconds the milliseconds get rounded to the nearest second.

               

            static void Test()
            {

                ADODB.Recordset rs = new ADODB.Recordset();
                rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient;
                ADODB.FieldAttributeEnum eAttributes = ADODB.FieldAttributeEnum.adFldUpdatable | ADODB.FieldAttributeEnum.adFldIsNullable | ADODB.FieldAttributeEnum.adFldMayBeNull | ADODB.FieldAttributeEnum.adFldFixed | ADODB.FieldAttributeEnum.adFldLong;
                rs.Fields.Append("MyDate", ADODB.DataTypeEnum.adDBTimeStamp, 8, eAttributes);
                rs.Open();

                rs.AddNew();

                DateTime expected = new DateTime(2004, 4, 5, 7, 19, 2, 577);
                rs.Fields["MyDate"].Value = expected;
                DateTime actual = Convert.ToDateTime(rs.Fields["MyDate"].Value);
                if (actual != expected)
                {
                    Console.WriteLine("Actual = {0}, Expected = {1}", actual.ToString("yyyy-MM-dd HH:mm:ss:fff"), expected.ToString("yyyy-MM-dd HH:mm:ss:fff"));
                }
            }

    The strange thing is that if I open the recordset using a query that returns a datetime with milliseconds then I do see the milliseconds in the Recordset field value (and I verified the field data type is adDBTimeStamp also), so it would seem like the adDBTimeStamp is the right data type for the Recordset field and it is capable of storing milliseconds but I can't programatically set the value to something with milliseconds.

    Any ideas?? 

    - Brad

    Monday, July 22, 2013 4:43 PM

Answers

All replies