简体   繁体   English

在C#中将日期值插入sql数据库

[英]Insert date value into sql database in C#

I have a problem when trying to insert the value of "datetimepicker" into column of type "date" 尝试将“ datetimepicker”的值插入“ date”类型的列时遇到问题

cmd = new SqlCommand("insert into PIECE_D_IDENDITE VALUES('" + textBox4.Text + "','" + dateTimePicker2.Value + "','" + textBox7.Text + "','" + comboBox1.SelectedValue + "');insert into PERSONNE (ID_CARTE,PRENOM_PERSONNE,NOM_PERSONNE,PROFESSION_PERSONNE,TEL_PERSONNE,ADRESSE_PERSONNE,DATE_NAIS_PERSONNE)values('" + textBox4.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox8.Text + "','" + textBox10.Text + "','" + textBox9.Text + "','" + dateTimePicker1.Value + "')", cn);
                cn.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("success!");

So the problem is when I choose the day's number greatest than 12 (for exemple 13/10/2016) this error appear => "Conversion failed when converting date and/or time from character string." 因此,问题在于当我选择大于12的日期(例如13/10/2016)时,出现此错误=>“从字符串转换日期和/或时间时转换失败。”

You must always use parameterized SQL, and do no conversion to string especially for Date\\DateTimes. 您必须始终使用参数化的SQL,并且不要转换为字符串,尤其是对于Date \\ DateTimes。

SqlCommand cmd = new SqlCommand(@"insert into PIECE_D_IDENDITE VALUES 
     (@v1, @v2, @v3, @v4);
insert into PERSONNE 
(ID_CARTE,PRENOM_PERSONNE,NOM_PERSONNE,
 PROFESSION_PERSONNE,TEL_PERSONNE,
 ADRESSE_PERSONNE,DATE_NAIS_PERSONNE)
values
(@ID_CARTE,@PRENOM_PERSONNE,@NOM_PERSONNE,
 @PROFESSION_PERSONNE,@TEL_PERSONNE,
 @ADRESSE_PERSONNE,@DATE_NAIS_PERSONNE);", cn);


cmd.Parameters.AddWithValue("@v1", textBox4.Text);
cmd.Parameters.AddWithValue("@v2", dateTimePicker2.Value);
cmd.Parameters.AddWithValue("@v3", textBox7.Text);
cmd.Parameters.AddWithValue("@v4", comboBox1.SelectedValue);

cmd.Parameters.AddWithValue("@ID_CARTE", textBox4.Text);
cmd.Parameters.AddWithValue("@PRENOM_PERSONNE", textBox1.Text);
cmd.Parameters.AddWithValue("@NOM_PERSONNE", textBox2.Text);
cmd.Parameters.AddWithValue("@PROFESSION_PERSONNE", textBox8.Text);
cmd.Parameters.AddWithValue("@TEL_PERSONNE", textBox10.Text);
cmd.Parameters.AddWithValue("@ADRESSE_PERSONNE", textBox9.Text);
cmd.Parameters.AddWithValue("@DATE_NAIS_PERSONNE", dateTimePicker1.Value);

cn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("success!");

In first insert you are not specifying the field names. 在第一次插入中,您未指定字段名称。 That would work but is depends on the order of the fields and dangerous IMHO (and I assumed all the fields other than dates were of type string, if not, convert them to their corresponding types). 那会起作用,但是取决于字段的顺序和危险的恕我直言(我假设除日期以外的所有其他字段均为字符串类型,如果不是,请将其转换为相应的类型)。

This problem is related to the local date configuration of your forms/server and the value returned. 此问题与您的表单/服务器的本地日期配置和返回的值有关。

If the local date format is MM/DD/YYY and you do not transform before call the procedure, the Day 13 will be stored into the MM format of the date into the database. 如果本地日期格式为MM / DD / YYY,并且您未在调用该过程之前进行转换,则Day 13将以该日期的MM格式存储到数据库中。

I have no clue about your MySQL Configuration or Winform configuration, but I am pretty sure that this problems is a different date format on client/server. 我对您的MySQL配置或Winform配置一无所知,但我可以肯定,此问题是客户端/服务器上的日期格式不同。

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

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