如何从DateTimePicker中选择时间并将日期时间值添加到mysql? [英] How to chose time from DateTimePicker and add datetime value to mysql?

查看:317
本文介绍了如何从DateTimePicker中选择时间并将日期时间值添加到mysql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。

我想从DateTimePicker中选择日期时间并将此值添加到MySQL。

在我的数据表中,我使用datetype创建列date_time类型。

然后我设置DateTimePicker的CustomFormat是dd-MM-yyyy。

我编写代码将日期时间值插入MySQL,但我的数据表中的值是00:00:00。

其他列是好的。

请帮我解决这个问题。谢谢。

这是我的代码

Hello.
I want to chose date-time from DateTimePicker and add this value to MySQL.
In my data table, I create column "date_time" with datetype type.
And then I set "CustomFormat" of DateTimePicker is "dd-MM-yyyy".
I write code to insert datetime value to MySQL, but value in my data table is 0000-00-00 00:00:00.
Orther column is good.
Please help me this problem. Thank you.
Here is my code

//connect_data();
           byte[] imageBt = null;
           if (txt_path.Text != "")
           {
               FileStream fstream = new FileStream(this.txt_path.Text, FileMode.Open, FileAccess.Read);
               BinaryReader br = new BinaryReader(fstream);
               imageBt = br.ReadBytes((int)fstream.Length);
           }
           else
           {
               imageBt = null;
           }

           String myConnection = "Server=localhost;Database=sctv_data;Port=3306;User ID=root;Password=;Charset=utf8";
           MySqlConnection conDatabase = new MySqlConnection(myConnection);
           string Query = "INSERT into nhap (STT,Ngay_nhap,Ten_thiet_bi,Nha_san_xuat,Don_vi,So_luong,Nhan_tu,Chip_ID,MAC_address,SN,Ghi_chu,Hinh) values ('" + txt_STT.Text + "','" +this.dateTimePicker1.Text+ "','" + txt_ten_tb.Text + "','" + txt_nha_sx.Text + "','" + txt_don_vi.Text + "','" + txt_so_luong.Text + "','" + txt_nhan_tu.Text + "','" + txt_chip_ID.Text + "','" + txt_mac.Text + "','" + txt_sn.Text + "','" + txt_note.Text + "',@hinh);";
           MySqlCommand cmdDataBase = new MySqlCommand(Query, conDatabase);
           MySqlDataReader myReader;
           try
           {
               conDatabase.Open();
               cmdDataBase.Parameters.Add(new MySqlParameter("@hinh", imageBt));
               myReader = cmdDataBase.ExecuteReader();
               MessageBox.Show("SAVE");
               while (myReader.Read())
               {
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

推荐答案

您需要做的第一件事就是不要将文字连接到SQL语句中。这样做会让您对SQL注入开放,并使处理值变得更加困难。而是使用 MySqlParameter [ ^ ]。



之后确保日期列包含在INSERT语句中。目前没有提到名为 date_time 的列,所以如果这是列的实际名称,插入后它将具有空值。
The first thing you need to do is not to concatenate literals into SQL statement. Doing so leaves you open to SQL injections and makes handling values much harder. Instead use MySqlParameter[^].

After that make sure that the date column is included in the INSERT statement. At the moment there is no column named date_time mentioned so if that is the actual name of the column, it will have empty value after insert.


这篇关于如何从DateTimePicker中选择时间并将日期时间值添加到mysql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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