简体   繁体   English

将日期转换为日期时间格式c#

[英]Convert date into datetime format c#

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 我认为它应该是某个地方,相同的SQL查询识别日期这是我设置一个断点时得到的结果 在此输入图像描述 This is the result at the base 这是基地的结果

在此输入图像描述

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;
}

Your screenshot shows a .Query<> method. 您的屏幕截图显示了一个.Query<>方法。 SqlConnection has no Query method. SqlConnection没有Query方法。

Dapper does, a microORM used to make writing parameterized queries easier . 小巧玲珑的那样,用来使编写参数化查询更容易 microORM。 You don't need string interpolation or concatenation when using Dapper or any other ORM. 使用小巧精致的或任何其他的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});

You are wrong in your approach. 你的做法是错的。 You should never create a SQL statement through string concatenation. 您永远不应该通过字符串连接创建SQL语句。 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. 您很容易使用SQL注入,您将在不同的系统上遇到格式化问题,并且您将对查询解析器的性能产生很大影响。

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); 你的SQL应该看起来像where ZPZ_Datum = @zpzdate ,然后你添加一个名为zpzdate的参数: command.Parameters.Add("@zpzdate", arravial); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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