Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert date into datetime format c#

Tags:

c#

datetime

I have a problem with converting datetime, I do not see where I make a mistake. I think it should be somewhere to date a date for the same sql query to recognize This is the result I get when I set up a break point enter image description here This is the result at the base

enter image description here

This is my code with which I send the data

var arravialDates = dataAccess.get_dates(userID, date, DateTime.DaysInMonth(date.Year, date.Month)); //get dates for user

//working hours

DateTime dateH = new DateTime(2099, 12, 12, 0, 0, 0);
bool hasDate = false;
for (int i = 0; i < arravialDates.Count; i++)
{
     var arravial = dataAccess.getPraesenzzeit(arravialDates[i], userID);
     int index = 0;
}
like image 766
Mara Avatar asked Feb 02 '18 08:02

Mara


2 Answers

Your screenshot shows a .Query<> method. SqlConnection has no Query method.

Dapper does, a microORM used to make writing parameterized queries easier. You don't need string interpolation or concatenation when using Dapper or any other ORM. Just write :

var output=connection.Query<PRAESENZZEIT>(
                "select * from Z_PRAESENZZEIT " + 
                "where ZPZ_Datum = @date and ZPZ_LPE_ID= @id "
                "order by ZPZ_ID_ASC", 
                new {date=DatTime.Today,id=1});

Better yet :

var query = "select * from Z_PRAESENZZEIT " + 
            "where ZPZ_Datum = @date and ZPZ_LPE_ID= @id "
            "order by ZPZ_ID_ASC", 

var output=connection.Query<PRAESENZZEIT>(query,new {date=date,id=userid});
like image 123
Panagiotis Kanavos Avatar answered Oct 23 '22 12:10

Panagiotis Kanavos


You are wrong in your approach. You should never create a SQL statement through string concatenation. You are prone to SQL injection, you will experience formatting problems on different systems, and you will have a high impact on the performance of the query parser.

The solution is to use parameterized queries: you add a placeholder for a parameter and add the value through the command parameters.

Your SQL should look like where ZPZ_Datum = @zpzdate, then you add a parameter with the name zpzdate: command.Parameters.Add("@zpzdate", arravial);.

like image 3
Patrick Hofman Avatar answered Oct 23 '22 14:10

Patrick Hofman