2

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 ??

Ahmed Aekbj
  • 91
  • 1
  • 12
  • Shouldn't this happen automaticly with the use of paramertized queries? – lokusking Jul 14 '16 at 10:59
  • 'managed to form a string date out of that ' go further convert it to c# DateTime, and pass this date to sql cmd in convenient way – Irdis Jul 14 '16 at 11:06

1 Answers1

0

You can use CAST :

"INSERT INTO Events_List (date, Object,Event,IOA,ASDU) VALUES(CAST(@date as DATETIME2), @event_object, @Event, @IOA, @ASDU)";
sagi
  • 40,026
  • 6
  • 59
  • 84