'輸入字符串格式不正確'。errro將數據插入數據庫時
我想用C#將數據插入到MYSQL數據庫,但我得到的是說
輸入字符串的不正確的格式錯誤消息。 (ExecuteNonQuery)時出現此錯誤。
public void Register_Cutomer_Orders()
{
string ConnStr = ConfigurationManager.ConnectionStrings["ConnSet"].ConnectionString;
string cmdstr = @"INSERT INTO `shopsorders`
(`order_id`,
`Customers_customer_id`,
`Employees_employee_id`,
`Shops_shop_id`,
`total`,
`date`)
VALUES
(@P_order_id,
@P_Customers_customer_id,
@P_Employees_employee_id,
@P_Shops_shop_id,
@P_total,
@P_date)";
try
{
using (MySqlConnection conn = new MySqlConnection(ConnStr))
using (MySqlCommand cmd = new MySqlCommand(cmdstr, conn))
{
conn.Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = cmdstr;
foreach (DataGridViewRow item in dGVShop.Rows)
{
cmd.Parameters.Clear();
cmd.Parameters.Add("@P_order_id", MySqlDbType.Int32).Value = null;
cmd.Parameters.Add("@P_Customers_customer_id", MySqlDbType.Int32).Value = Convert.ToInt32(TB_Shop_ReservNum.Text);
cmd.Parameters.Add("@P_Employees_employee_id", MySqlDbType.Int32).Value = 1;
cmd.Parameters.Add("@P_Shops_shop_id", MySqlDbType.Int32).Value = Convert.ToInt32(cbShop_Name.SelectedValue);
cmd.Parameters.Add("@P_total", MySqlDbType.Double).Value = Convert.ToDouble(tb_Shop_Total.Text);
cmd.Parameters.Add("@P_date", MySqlDbType.DateTime).Value = "sysdate()";
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
嘗試的日期參數 – Turo
一個真正的日期。如果你總是希望在'date'然後把SYSDATE在SELECT中,不要打擾使用它的參數。 – Crowcoder