简体   繁体   中英

Inserting unique values from datagridview to mysql database table

I have a datagridview that successfully inserts the data from datagrid to mysql table. But it just inserts rows that already exists in the mysql table. If I assign primary keys in mysql table, it throws me the error that Primary key cannot be duplicated. Can anyone help to generate this query either by using NOT EXISTS, DISTINCT, or anything else.

My code is:

 using (MySqlCommand cmd = new MySqlCommand("INSERT INTO markeddeleted VALUES(@product_id, @product_name, @category_id)", con))

You could use one of these:

INSERT IGNORE INTO markeddeleted VALUES(@product_id, @product_name, @category_id)

That will ignore errors (but will ignore other errors too).

INSERT INTO markeddeleted (product_id,product_name,category_id) VALUES(@product_id, @product_name, @category_id) ON DUPLICATE KEY UPDATE product_name=@product_name

That will update the rows where the duplicate keys exist.

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