简体   繁体   English

如何使用datetime列类型将datetimepicker默认值插入MYSQL?

[英]How to insert datetimepicker default value to MYSQL with datetime column type?

I have 2 project, one is CRUD class and another is contain a WinForm that will access the CRUD class. 我有2个项目,一个是CRUD类,另一个是包含将访问CRUD类的WinForm。 Here come my problem, everytime i trying to insert DateTimePicker with default value in MYSQL with datetime column type, i getting 0000-00-00 00:00:00 Zero zero zero and zero value, what i missed here? 这是我的问题,每当我尝试在具有日期时间列类型的MYSQL中插入具有默认值的DateTimePicker时,我得到了0000-00-00 00:00:00零零零和零值,我在这里错过了什么?

Here is my part of CRUD class code, my CRUD class named DBConnect.cs 这是我的CRUD类代码的一部分,我的CRUD类名为DBConnect.cs

public DateTime property_dtDate { get; set; }

sqlQuery = "INSERT INTO master (NoInvoice, Name, Date, Type, Additionaltext) VALUES ('" + property_NoInvoice + "', '" + property_sName + "', '" + property_dtDate + "', '" + property_sType + "', '" + property_sAdditionalText + "')"; 

And here is my part of WinForm code, which is i named it Form1.cs (both is in separated project) 这是WinForm代码的一部分,我将其命名为Form1.cs(均在单独的项目中)

clsDbConnect.property_dtDate = DateTime.Parse(datetimepicker1.Value.ToString("yyyy-MM-dd HH:mm:ss"));

clsDbConnect.Insert();

I try look at the value with messagebox, a value that pass through my WinForm is good, nothing suspicious, it show the every date and time which is right know, but when i looked in my database, all i got is 0000-00-00 00:00:00. 我尝试用消息框查看值,通过我的WinForm传递的值很好,没有可疑之处,它显示了正确的每个日期和时间,但是当我查看数据库时,我得到的只是0000-00-00 00:00:00. I don't have any clue what cause that, help me to find out please 我不知道是什么原因,请帮助我找出原因

I think you have a problem because of string format of the datetime. 我认为您有问题,因为datetime的字符串格式。 MySQL not understand your string format as a datetime value. MySQL无法将您的字符串格式理解为datetime值。
Try use parameters(assume somewhere in code you have a SqlCommand object): 尝试使用参数(假设代码中的某个地方有SqlCommand对象):

string query = "INSERT INTO master (NoInvoice, Name, Date, Type, Additionaltext) VALUES (@NoInvoice, @sName, @dtDate, @sType, @sAdditionalText)";
using(SqlCommand command = New SqlCommand(query, yourconnection)
{
    command.Parameters.AddWithValue("@NoInvoice", property_NoInvoice);
    command.Parameters.AddWithValue("@sName ", property_sName);
    command.Parameters.AddWithValue("@dtDate ", property_dtDate);
    command.Parameters.AddWithValue("@sType ", property_sType);
    command.Parameters.AddWithValue("@sAdditionalText ", property_sAdditionalText);

    command.ExecuteNonQuery();
}

Using parameters help with type safety(datetime and numerics format issues) and prevent a SQL injection vulnerability. 使用参数有助于类型安全(日期时间和数字格式问题)并防止SQL注入漏洞。

Then when you don't need to parse a datetimepicker 's value to type DateTime because ti is already DateTime ( From MSDN: DateTimePicker.Value ) 然后,当您不需要解析datetimepicker的值以键入DateTime因为ti已经是DateTime来自MSDN:DateTimePicker.Value

clsDbConnect.property_dtDate = datetimepicker1.Value;

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

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