我有一个程序,可以在数据库中插入字段列表。当我使用自己的计算机插入日期时间字段时,它看起来很好。但是,当我使用Windows 7中文版插入它时,该字段成为00:00:00 这是命令
MySqlCommand myCommand4 = new MySqlCommand("Insert into OrderRecords_table values('" + OrderIDLabel.Text + "','" + customerCode + "','" + customer + "','" + TelComboBox.Text + "','" + LicenseComboBox.Text + "','" +
DriverComboBox.Text + "','" + AddressComboBox.Text + "','" + LocationTypeComboBox.Text + "','" + PickupComboBox.Text + "','" + CustomerTypeLabel.Text + "','" +
Convert.ToDecimal(TotalPriceLabel.Text) + "','" + status + "','" + note + "','" + sandReceiptNo + "','" + createtiming + "','" + Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) + "')", myConnection);
myCommand4.ExecuteNonQuery();
我知道它看起来有点乱,但它说的部分
STR_TO_DATE('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','%Y/%M/%d /%H/%m/%s'))"
是我插入当前日期时间的部分。当我使用英文版的Windows时它工作得很好,但每当我使用中文版时,它是有效的00:00:00而不是实际时间,我试图改变控制面板中显示日期的格式,但它仍然有同样的问题。
由于
编辑了我的代码
var sql = "insert into OrderRecords_table values(@OrderID, @customercode, @customer, @PhoneNumber, @license, @driver, @address, @type, @pickupLocation, @PaymentMethod, @totalPrice, @status, @notes, @sandreceiptNo,@createTime, @EditTime)";
using (var myCommand4 = new MySqlCommand(sql, myConnection))
{
myCommand4.Parameters.AddWithValue("@orderId", MySqlDbType.VarChar).Value = OrderIDLabel.Text;
myCommand4.Parameters.AddWithValue("@customercode", MySqlDbType.VarChar).Value = customerCode;
myCommand4.Parameters.AddWithValue("@customer",MySqlDbType.VarChar).Value = customer;
myCommand4.Parameters.AddWithValue("@PhoneNumber", MySqlDbType.VarChar).Value =TelComboBox.Text;
myCommand4.Parameters.AddWithValue("@license", MySqlDbType.VarChar).Value = LicenseComboBox.Text;
myCommand4.Parameters.AddWithValue("@driver", MySqlDbType.VarChar).Value = DriverComboBox.Text;
myCommand4.Parameters.AddWithValue("@address", MySqlDbType.VarChar).Value = AddressComboBox.Text;
myCommand4.Parameters.AddWithValue("@Type", MySqlDbType.VarChar).Value = LocationTypeComboBox.Text;
myCommand4.Parameters.AddWithValue("@pickupLocation", MySqlDbType.VarChar).Value = PickupComboBox.Text;
myCommand4.Parameters.AddWithValue("@PaymentMethod", MySqlDbType.VarChar).Value = CustomerTypeLabel.Text;
myCommand4.Parameters.AddWithValue("@totalPrice", MySqlDbType.Decimal).Value = Convert.ToDecimal(TotalPriceLabel.Text);
myCommand4.Parameters.AddWithValue("@status", MySqlDbType.VarChar).Value = status;
myCommand4.Parameters.AddWithValue("@notes", MySqlDbType.VarChar).Value =status;
myCommand4.Parameters.AddWithValue("@sandreceiptNo", MySqlDbType.VarChar).Value = sandReceiptNo;
myCommand4.Parameters.AddWithValue("@createTiming", MySqlDbType.DateTime).Value = createtiming;
myCommand4.Parameters.AddWithValue("@EditTime", MySqlDbType.DateTime).Value = DateTime.Now;
myCommand4.ExecuteNonQuery();
它说我有一些无效的输入,但我已经检查了几次所有字段都被赋予了正确的类型...不知道发生了什么
答案 0 :(得分:0)
我已将您的代码重写为更标准化的实施(使用最佳做法)。请注意,我已将您的查询拉出到一个单独的变量中,以使代码和查询更具可读性。
var sql = "insert into orderrecords_table values " +
"(@orderId, " +
" @customercode, " +
" @customer, " +
" @telephone, " +
" @license, " +
" @driver, " +
" @address " +
" @locationType, " +
" @pickup, " +
" @customerType, " +
" @totalPrice, " +
" @status, " +
" @note, " +
" @sandreceiptNo, " +
" @createTiming, " +
" @currentTime) ";
using (var myCommand4 = new MySqlComm## Heading ##and(sql, connection))
{
myCommand4.Parameters.AddWithValue("@orderId", OrderIDLabel.Text) ;
myCommand4.Parameters.AddWithValue("@customercode", customerCode);
myCommand4.Parameters.AddWithValue("@customer", customer);
myCommand4.Parameters.AddWithValue("@telephone", TelComboBox.Text);
myCommand4.Parameters.AddWithValue("@license", LicenseComboBox.Text);
myCommand4.Parameters.AddWithValue("@driver", DriverComboBox.Text);
myCommand4.Parameters.AddWithValue("@address", AddressComboBox.Text);
myCommand4.Parameters.AddWithValue("@locationType", LocationTypeComboBox.Text);
myCommand4.Parameters.AddWithValue("@pickup", PickupComboBox.Text);
myCommand4.Parameters.AddWithValue("@customerType", CustomerTypeLabel.Text);
myCommand4.Parameters.AddWithValue("@totalPrice", Convert.ToDecimal(TotalPriceLabel.Text));
myCommand4.Parameters.AddWithValue("@status", status);
myCommand4.Parameters.AddWithValue("@note", status);
myCommand4.Parameters.AddWithValue("@sandreceiptNo", sandReceiptNo);
myCommand4.Parameters.AddWithValue("@createTiming", createtiming);
myCommand4.Parameters.AddWithValue("@currentTime", DateTime.Now);
myCommand4.ExecuteNonQuery();
}
答案 1 :(得分:-1)
MySqlCommand Insert = new MySqlCommand("INSERT INTO [TABLE] ([Date], [TEXT]) VALUES(@Date, @Text) ", myConnection);
Insert.CommandTimeout = 60; //if you need
Insert.Parameters.AddWithValue("@Date", DateTime.Now);
Insert.Parameters.AddWithValue("@Text", "Hello word!");
Insert.ExecuteNonQuery();
Insert.Dispose();