mysql酒店入住日期选择的语句,如何从DateTimePicker中选择时间并将日期时间值添加到mysql?...

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);

}

解决方案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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值