简体   繁体   中英

c# net connector insert into mysql system datetime error

i am using mysql net connector and i want to insert some data , without datetime it works but with date time it gives error. my code is;

da.InsertCommand = new MySqlCommand("INSERT INTO orders( VALUES('',@ORDER_DATE, @DATE_SHIPMENT, @PRODUCT_ID, @QUANTITY, @CUSTOMER_ID, @INVOICE_FEE, @PROD_TYPE, @BRAND, @MODEL, @PRICE, @VAT)", cs);
da.InsertCommand.Parameters.Add("ORDER_DATE", MySqlDbType.DateTime).Value = oRDER_DATEDateTimePicker.Text;
da.InsertCommand.Parameters.Add("DATE_SHIPMENT", MySqlDbType.DateTime).Value = dATE_SHIPMENTDateTimePicker.Text;
da.InsertCommand.Parameters.Add("PRODUCT_ID", MySqlDbType.Int32).Value = pRODUCT_IDTextBox.Text;
da.InsertCommand.Parameters.Add("QUANTITY", MySqlDbType.Decimal).Value = qUANTITYTextBox.Text;
da.InsertCommand.Parameters.Add("CUSTOMER_ID", MySqlDbType.Int32).Value = textiD.Text;
da.InsertCommand.Parameters.Add("INVOICE_FEE", MySqlDbType.VarChar).Value = comboBoxfee.Text;
da.InsertCommand.Parameters.Add("PROD_TYPE", MySqlDbType.VarChar).Value = pROD_TYPETextBox.Text;
da.InsertCommand.Parameters.Add("BRAND", MySqlDbType.VarChar).Value = bRANDTextBox.Text;
da.InsertCommand.Parameters.Add("MODEL", MySqlDbType.VarChar).Value = mODELTextBox.Text;
da.InsertCommand.Parameters.Add("PRICE", MySqlDbType.Decimal).Value = pRICETextBox.Text;
da.InsertCommand.Parameters.Add("VAT", MySqlDbType.Decimal).Value = vATTextBox.Text;


cs.Open();
da.InsertCommand.ExecuteNonQuery();
cs.Close();

error is: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VALUES('','0007-12-2012 00:00:00 ', '0007-12-2012 00:00:00

i guess my datetime format is not recognizing by mysql,in my winform oRDER_DATEDateTimePicker.Text and dATE_SHIPMENTDateTimePicker.Text is short datetime.

thanks

Instead of adding (as a single example of a wider problem) dATE_SHIPMENTDateTimePicker.Text , use DateTime.Parse (etc) to get the actual value as a DateTime , and add that:

var when = DateTime.Parse(dATE_SHIPMENTDateTimePicker.Text);
da.InsertCommand.Parameters.Add(
   "DATE_SHIPMENT", MySqlDbType.DateTime).Value = when;

The same applies to all the parameters; integers, dates, decimals, etc. In fact, simply having database code (commands etc) and UI code (text-boxes) in the same method tells me something is very wrong: ideally, you would have that via a method somewhere that takes typed parameters:

void CreateOrder(int foo, string bar, DateTime baz, decimal blop, ...)
{
   ...
}

It is the job of the UI to turn the human input into real values that make sense to other layers, such as your data-access code.

So done properly , the UI would handle the parsing, and then call a separate method that knows nothing about the UI to talk to the database.

Another approach is for the UI to build an object with typed members and pass that in:

void CreateOrder(Order order)
{
    ...
}

Then the UI does:

var order = new Order();
order.Id = /* todo... */
/* ...for each property... */
CreateOrder(order);

MySqlDbType.DateTime wants DateTime as parameter, and not string .

Use DateTime.Parse(oRDER_DATEDateTimePicker.Text) or DateTime.ParseExact(oRDER_DATEDateTimePicker.Text, format) where format is custom format for date that you choose. It can be "yyyy-DD-MM" or whatever else you want or need.

You seem to have a few typos in your query:

da.InsertCommand = new MySqlCommand("INSERT INTO orders( VALUES('',@ORDER_DATE, @DATE_SHIPMENT, @PRODUCT_ID, @QUANTITY, @CUSTOMER_ID, @INVOICE_FEE, @PROD_TYPE, @BRAND, @MODEL, @PRICE, @VAT)", cs);
                                                      ^^^     ^^

Put a space between VALUES and ( , and remove the parenthesis after orders :

da.InsertCommand = new MySqlCommand("INSERT INTO orders VALUES ('',@ORDER_DATE, @DATE_SHIPMENT, @PRODUCT_ID, @QUANTITY, @CUSTOMER_ID, @INVOICE_FEE, @PROD_TYPE, @BRAND, @MODEL, @PRICE, @VAT)", cs);

Second, (as others have mentioned), you are not be using the correct DateTime format. MySQL will accept DateTime.Parse as an input, but it should also accept a string in this format:

yyyy-MM-dd HH:mm:ss

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM