Search code examples
c#ms-accessdatetimepicker

How to insert only time in ms access?


This is the query I am having issues with:

cmd = new OleDbCommand(insert into tbl_Customer(cReportingTime) values (@ReportingTime)", con);
cmd.Parameters.Add("@ReportingTime", OleDbType.DBTime).Value = Time;
cmd.ExecuteNonQuery();

When I try to run it I am getting this error:

"Failed to convert parameter value from a DateTime to a TimeSpan"

I want to insert only time in MS Access database however I can't seem to get it to work.


Solution

  • I assume your Time is a DateTime, you can use it's TimeOfDay property like;

    cmd.Parameters.Add("@ReportingTime", OleDbType.DBTime).Value = Time.TimeOfDay;
    

    Since DBTYPE_DBTIME mapped with TimeSpan, this should work.