简体   繁体   中英

Convert a string to datetime2

Iam using an application that gets data through a communication protocol , and among data i get is dates and i managed to form a string date out of that in this form :

"dd/MM/yyyy HH:mm:ss.mmm"

I want to insert it in the database :

updateEvent_list = "INSERT INTO Events_List (date, Object,Event,IOA,ASDU) VALUES(@date, @event_object, @Event, @IOA, @ASDU)";

The date column is of datetime2 type .

Question is : how can i convert the string i get into datetime2 ???

EDIT :

In fact i have two types of dates to insert : 1) Frames that come with time tag : the ones that i convert their dates to string. 2) Frames that come without timetag : Iam using DateTime date =DateTime.UtcNow

Each of these work perfectly when they implemented independantly

Is there any solution that permits using both in the same program ??

您可以使用CAST

"INSERT INTO Events_List (date, Object,Event,IOA,ASDU) VALUES(CAST(@date as DATETIME2), @event_object, @Event, @IOA, @ASDU)";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM