【问题标题】:error while insert data into database将数据插入数据库时​​出错
【发布时间】:2017-05-27 13:03:28
【问题描述】:

'输入字符串的格式不正确。'

我正在尝试使用 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);
        }

}

【问题讨论】:

  • 为日期参数尝试一个真实的日期
  • 如果你总是希望 sysdate 在 date 中,那么把它放在 SELECT 中,不要为它使用参数。

标签: c# mysql


【解决方案1】:

sysdate() 不是可以转换为日期的字符串。如果您要发送当前时间,请使用DateTime.UtcNow

cmd.Parameters.AddWithValue("@P_date", MySqlDbType.DateTime).Value = DateTime.UtcNow;

在执行隐式转换时,数据库不会执行字符串标记 - 它只是尝试将其转换为所需的类型。

还有:@P_Customers_customer_id@P_Shops_shop_id@P_total 都是数字,但给出了文本值,这是一个非常糟糕的主意

【讨论】:

    【解决方案2】:

    AddWithValue的第二个参数不是参数的类型而是值。

    cmd.Parameters.AddWithValue("@P_Customers_customer_id", TB_Shop_ReservNum.Text);
    

    如您所见,这仍然很弱,因为您传递一个字符串,AddWithValue 决定查看接收到的输入的参数类型。

    这篇博文解释了 AddWithValue 的所有弱点 Can we stop using AddWithValue already?

    我更喜欢这样使用Add方法

    cmd.Parameters.Add("@P_Customers_customer_id", MySqlDbType.Int32).Value = Convert.ToInt32(TB_Shop_ReservNum.Text);
    

    最后,当您传递字符串“sysdate()”时,它不会在系统日期中解析,而是在文字字符串中解析,显然文本“sysdate()”不是日期。

    在这种情况下,您应该使用存储过程来获取服务器上的系统日期或传递本地计算机 DateTime.Now

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-25
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 2019-10-11
      • 2016-08-23
      相关资源
      最近更新 更多