简体   繁体   English

我想从C#中的DateTimePicker对象更新我的数据库表中的“Datetime”数据类型的字段

[英]I want to update field with “Datetime” datatype in table of my database from DateTimePicker object in C#

Note: Don't care with Connection becuse the connection work. 注意:请不要关心Connection,因为连接工作正常。 Field in database is DateTime 数据库中的字段是DateTime

DateTime dtc = Convert.ToDateTime(dateTimePicker1.Value.Date);

cmd = new SqlCommand("UPDATE LAB_TESTING set Lab_Nam='" + lab_id + "',Rslt_lb='" + 
                     textBox1.Text + "',Tst_Dat='" + dtc + "' Where Lab_ID='" + 
                     bindex + "'", con);
con.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("You Update recored successfully", "delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
dataGridView1.DataSource = null;
con.Close();
readdatagrid1();

after Run program , I got Conversion failed when converting date and/or time from character string. 在运行程序后,我从字符串转换日期和/或时间时转换失败。

Don't use string concatenation to build your queries ( SQL injection alert , also read this !). 不要使用字符串连接来构建您的查询( SQL注入警报 ,也请阅读此内容 !)。 Use parameterized queries . 使用参数化查询 As for your problem: format the date as yyyy-MM-dd ( ISO 8601 ) and it'll work. 至于你的问题:将日期格式化为yyyy-MM-ddISO 8601 )并且它将起作用。

If you'd have used parameterized queries correctly you could've just passed in the DateTime as-is and the driver would've ensured the value would get passed correctly to SQL regardless the "format" / locale setting / whatever since the value would be passed as a DateTime instead of a string. 如果您已经正确使用了参数化查询,那么您可以按原样传入DateTime,并且驱动程序将确保无论“格式”/语言环境设置/因为值是什么,驱动程序都会正确地传递给SQL作为DateTime而不是字符串传递。 Something like this: 像这样的东西:

cmd = new SqlCommand(@"UPDATE LAB_TESTING set Lab_Nam = @labnam,
                            Rslt_lb = @result, Tst_Dat = @tstdat
                            Where Lab_ID = @id", con);

cmd.Parameters.AddWithValue("@labnam", lab_id );
cmd.Parameters.AddWithValue("@result", textBox1.Text);
cmd.Parameters.AddWithValue("@tstdat", dateTimePicker1.Value.Date);
cmd.Parameters.AddWithValue("@id", bindex);

con.Open();
cmd.ExecuteNonQuery();

Other than that I also recommend to wrap the con and cmd in a using() { ... } statement so that you can be sure this stuff gets disposed properly and that you give your variables and fieldnames decent names and use a consistent naming scheme. 除此之外,我还建议将concmd包装在using() { ... }语句中,这样您就可以确保这些东西得到正确处理,并且您可以为变量和字段名称提供正确的名称并使用一致的命名方案。

Now repeat after me: 现在重复我:

  • I will never, ever, run queries again that have been string-concatenated together anymore! 我永远不会再次运行已经串联在一起的查询了!

  • From now on I will use parameterized queries 从现在开始,我将使用参数化查询

  • If I need to run string-concatenated queries ever again I will make sure all values are escaped properly 如果 我需要再次运行字符串连接查询,我将确保所有值都正确转义

Repeat the above aloud, at least 50 times. 大声重复上述内容,至少50次。

Try this 尝试这个

DateTime dtc = Convert.ToDateTime(dateTimePicker1.Value.ToString("dd/MM/yyyy"));

or else 要不然

you can also do this 你也可以这样做

DateTime dtc = Convert.ToDateTime(dateTimePicker1.Text)

If you are getting this error with your SQL code than have a look here 如果您的SQL代码出现此错误,请查看此处

这对你有用。

    var date = (DateTime)dateTimePicker1.SelectedDate.Value;

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

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