使用指定格式插入日期时间选择器值? [英] Insert into datetime picker values with specified format?

查看:98
本文介绍了使用指定格式插入日期时间选择器值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Datum sql format is date
Vrijeme poziva is sql format time(7)







 Working fine but in datagrid have values 
09.05.2018 00:00:00
09:51:32.25655565
 
In sql value is 2018-05-09
09:51:32.25655565
 
I need 
 
09.05.2018
09:51:32
 
Some help?





我的尝试:





What I have tried:

"INSERT INTO dbo.registar (datum, vrijeme_poziva)" +  
" VALUES ('" + datumDateTimePicker.Value.Date.ToString("yyyyMMdd") + "', '" + vrijeme_pozivaDateTimePicker.Value.TimeOfDay.ToString("HHmmss") + "')";  

推荐答案

这里有两件事是错的,我怀疑它们是相关的。

首先是您正在将一个非常好的DateTime值转换为字符串并通过字符串连接将其发送到SQL - 这意味着您的其余代码也使用连接。这很危险,非常危险!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

THere are two things wrong here, and I suspect they are related.
The first is that you are converting a perfectly good DateTime value to a string and sending that to SQL via string concatenation - and that implies that the rest of your code uses concatenation as well. That's dangerous, very dangerous! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期进行备份,不是吗?



第二种是转换为字符串意味着你将日期作为字符串值存储在数据库中 - 这也是一个坏主意。如果您使用DATE,DATETIME或DATETIME2列,则可以将格式应用于DataGrid,它将直接以您想要的任何格式从DB自动显示DateTime值。作为一个字符串,它变得更加复杂,因为你需要先将日期解析为DateTime,然后将其格式化为字符串以便显示并且变得非常混乱 - 因为我保证你会以某种方式获得无效的日期进入你的数据库 - 它总是这样 - 你必须在你的解析中允许它。



将你的SQL注入作为一个非常高的优先级排序,然后得到你的DB正确 - 然后开始考虑显示格式!

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

The second is that the conversion to a string implies that you are storing your dates in the DB as string values - and that's a bad idea as well. If you use DATE, DATETIME, or DATETIME2 columns instead, then you can apply a format to the DataGrid which will automatically display the DateTime value from the DB directly in any format you want. As a string, it gets a lot more complicated, because you need to parse the date to a DateTime first, and then format it back to a string for display and that gets very messy - because I guarantee you that somehow an invalid date will get into your DB - it always does - and you have to allow for that in your parsing.

Sort out your SQL Injection as a very high priority, and then get your DB right - then start thinking about display formats!


这篇关于使用指定格式插入日期时间选择器值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆