I have a string like this: 16:00 and I want it to be saved in my SQL Server database in a column which has a data type of time(7)…
Of course, before I save it in there, I need to convert my string in a time data type.
Upon using Convert.ToDateTime, I get an error:
Cannot implicitly convert type ‘System.DateTime’ to ‘System.TimeSpan’
That is because I am saving Convert.ToDateTime(myString) into a property of an entity from my database which has time(7) data type…
Are there other ways of converting my string into a format which is compatible with the time(7) datatype in SQL Server?
The managed type that corresponds to the SQL
timedata type isTimeSpan(orTimeSpan?when nullable), notDateTime– refer to Mapping CLR Parameter Data for the list of type conversions.You can use
TimeSpan.Parse(myString)to convert your string.