Yes, if you're executing a DELETE statement, ExecuteNonQuery() on your MySqlCommand instance is what you want to use. It's also what you'd use if you're executing an INSERT or UPDATE statement or a stored procedure that doesn't return a resultset.
ExecuteNonQuery with DeleteCommand?
-
26-06-2022 - |
Question
I need use ExecuteNonQuery at those syntax's below? And they are correct? Objective use the da.DeleteCommand; which syntax's I gain performance?
MySqlCommand cmd = new MySqlCommand("DELETE FROM users_login WHERE UserID = @UserID;", coon);
cmd.Parameters.Add("@UserID", MySqlDbType.Int16).Value = oUsuario.UserID;
MySqlDataAdapter da = new MySqlDataAdapter();
da.DeleteCommand = cmd;
coon.Open();
ok = true;
/**OR use this**/
MySqlCommand cmd = new MySqlCommand("DELETE FROM users_login WHERE UserID = @UserID;", coon);
cmd.Parameters.Add("@UserID", MySqlDbType.Int16).Value = oUsuario.UserID;
coon.Open();
cmd.ExecuteNonQuery();
ok = true;
Solution
OTHER TIPS
In case you are using a nonquery but the delete won't work... you may need to use a transaction:
coon.Open();
var trans = coon.BeginTransaction();
var query = "DELETE FROM users_login WHERE UserID = @UserID;";
MySqlCommand cmd = new MySqlCommand(query, coon, trans);
cmd.Parameters.Add("@UserID", MySqlDbType.Int16).Value = oUsuario.UserID;
cmd.ExecuteNonQuery();
trans.Commit();
coon.Close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow