如何使用C#窗体在sql中使用AMPM插入日期 [英] how to insert date with AMPM in sql using C# windows form

查看:136
本文介绍了如何使用C#窗体在sql中使用AMPM插入日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 cmd.Parameters.AddWithValue(Date,VdateTimePicker1.Value.ToString(dd / mm / yyyy h:mm:ss tt)); 



i我使用上面的代码但没有工作。

解决方案

不要。



说真的,不要。

使用AM / PM指示器存储日期的唯一方法是将其存储为字符串 - 这是一个非常糟糕的主意。麻烦的是,这意味着每次要使用数据时都必须重新评估数据并将其转换为日期 - 这意味着错误的数据(不同的格式,错误的日期,用户名而不是日期)很容易进入数据库并且以后很难对其进行排序,因为它会导致您的应用程序在尝试使用它时崩溃。



将它们存储为DATETIME值,将它们作为DateTime参数发送,并将其自行排除。如果您稍后在以某种形式将其显示给用户时需要使用AM / PM指示器显示它们,那么请对它们进行格式化 - 因为您可以使用与该用户相关的格式(可能与该用户不同)格式化坐在他旁边的用户预期)。



那么你的代码就变成了:

 cmd。 Parameters.AddWithValue(  Date,VdateTimePicker1.Value); 


cmd.Parameters.AddWithValue("Date", VdateTimePicker1.Value.ToString("dd/mm/yyyy h:mm:ss tt"));


i am using above code but not working.

解决方案

Don't.

Seriously, don't.
The only way to store a date anywhere with an AM/PM indicator is to store it as a string - and that's a very poor idea. The trouble is that it means the data must be re-evaluated and converted to a date each time you want to use it - which means it's very easy for the wrong data (different format, bad dates, usernames instead of dates) to get into the database and very difficult to sort it out later because it causes your app(s) to crash only when they try to use it.

Store them as a DATETIME value, send them as a DateTime parameter and it all sorts itself out. If you need to display them with an AM/PM indicator when you later present them to the user in some form, then format them then - because you can present them in a format that is relevant to that user (which may be different to the format the user sitting next to him expects).

Then your code just becomes:

cmd.Parameters.AddWithValue("Date", VdateTimePicker1.Value);


这篇关于如何使用C#窗体在sql中使用AMPM插入日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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