输入字符串的格式不正确c#datetimepicker

时间:2013-12-14 01:03:25

标签: c# mysql string datetimepicker

private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (_action == "edit")
                {
                    update(_id, int.Parse(cbSupplier.ValueMember), dtpTRXdate.Value.ToString("yyyy-MM-dd"), dtpDUEdate.Value.ToString("yyyy-MM-dd"), txtRemarks.Text.ToString(), _conn);
                }
                else
                {
                    insert(int.Parse(cbSupplier.ValueMember), dtpTRXdate.Value.ToString("yyyy-MM-dd"), dtpDUEdate.Value.ToString("yyyy-MM-dd"), txtRemarks.Text.ToString(), _conn);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
       }

public void insert(int sup_ID, string TRX_date, string DUE_date, string remarks, MySqlConnection conn)
        {
            MessageBox.Show(sup_ID.ToString() + " " + TRX_date + " " + DUE_date + " " + remarks);

            MySqlCommand command = new MySqlCommand();

            command.Connection = conn;
            command.CommandText = "INSERT INTO PO_HEADER VALUES(null," + sup_ID + ",'" + TRX_date + "','" + DUE_date + "','" + remarks + "')";

            command.ExecuteNonQuery();
        }

public void update(int id, int sup_id, string trx_date, string due_date, string remarks, MySqlConnection conn)
        {
            MySqlCommand command = new MySqlCommand();

            command.Connection = conn;
            command.CommandText = "UPDATE PO_HEADER SET SUPPLIER_ID=" + sup_id + ", TRX_DATE='" + trx_date + "', DUE_DATE='" + due_date + "', REMARKS='" + remarks + "' WHERE ID=" + id;

            command.ExecuteNonQuery();
        }

dtpTRXdate它是datetimepicker
问题出在:dtpTRXdate.Value.ToString(“yyyy-MM-dd”)和dtpDUEdate.Value.ToString(“yyyy-MM-dd”)
当我点击按钮保存并运行该功能时,它说“输入字符串格式不正确”
我在消息框中输入字符串,例如:“2012-12-12”
有任何想法???

1 个答案:

答案 0 :(得分:0)

问题:在Date转换为DateTimePicker string后,您正在发送从yyyy-MM-dd控件中选择的database值,但在{{1} } datatype表可能是Date,因此两者都需要Date and Time

解决方案:您需要将DateDateTimePicker控件中选择转换为Date and Time,而不是仅转换为Date

试试这个:

dtpTRXdate.Value.ToString("yyyy-MM-dd HH:mm:ss")

建议:使用parameterised queries,您无需担心types被传递,因为默认情况下会照顾它。

使用parameterised queries,您可以避免SQL Injection Attacks

使用parameterised queries

完成代码
private void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (_action == "edit")
            {
                update(_id, int.Parse(cbSupplier.ValueMember), dtpTRXdate.Value, dtpDUEdate.Value, txtRemarks.Text.ToString(), _conn);
            }
            else
            {
                insert(int.Parse(cbSupplier.ValueMember), dtpTRXdate.Value, dtpDUEdate.Value, txtRemarks.Text.ToString(), _conn);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
   }

public void insert(int sup_ID, DateTime TRX_date, DateTime DUE_date, string remarks, MySqlConnection conn)
    {
        MessageBox.Show(sup_ID.ToString() + " " + TRX_date.ToShortDateSTring() + " " + DUE_date.ToShortDateSTring() + " " + remarks);

        MySqlCommand command = new MySqlCommand();

        command.Connection = conn;
        command.CommandText = "INSERT INTO PO_HEADER VALUES(@value1,@sup_ID,@TRX_date,@ DUE_date,@remarks)";
        command.Parameters.AddWithValue("@value1",DBNull.Value);
        command.Parameters.AddWithValue("@sup_ID",sup_ID);
        command.Parameters.AddWithValue("@TRX_date",TRX_date);
        command.Parameters.AddWithValue("@DUE_date",DUE_date);
        command.Parameters.AddWithValue("@remarks",remarks);
        command.ExecuteNonQuery();
    }

public void update(int id, int sup_id, string trx_date, string due_date, string remarks, MySqlConnection conn)
    {
        MySqlCommand command = new MySqlCommand();

        command.Connection = conn;
        command.CommandText = "UPDATE PO_HEADER SET SUPPLIER_ID=@sup_id,TRX_DATE=@trx_date,DUE_DATE=@due_date,REMARKS=@remarks WHERE ID=@id";

        command.Parameters.AddWithValue("@sup_ID",sup_ID);
        command.Parameters.AddWithValue("@trx_date",trx_date);
        command.Parameters.AddWithValue("@due_date",due_date);
        command.Parameters.AddWithValue("@remarks",remarks);
        command.Parameters.AddWithValue("@sup_ID",id);
        command.ExecuteNonQuery();
    }