我有一个c程序,它试图将数据插入mysql数据库。数据可能已经在数据库中,在这种情况下,它只是覆盖它。例子:
MySqlConection dbConnection = ...; // Setup connection
int numCustomersToUpdate = 3;
String query =
"INSERT INTO customer_regions (customer_id, region_id)
VALUES (1, 205), (2, 314), (3, 151) // 3 customers
ON DUPLICATE KEY UPDATE region_id = VALUES(region_id)";
MySqlCommand cmd = new MySqlCommand(query, dbConnection);
int rowsAffected = cmd.ExecuteNonQuery();
if (rowsAffected == numCustomersToUpdate)
{
// Data either did not exist before and was inserted,
// or already existed and overwritten
}
else
{
// Error inserting data
}
问题是如果数据被覆盖, rowsAffected
为0,这被视为错误。不管数据是被插入还是被覆盖,我怎么能认为它是一个成功的sql执行呢?
暂无答案!
目前还没有任何答案,快来回答吧!