문제

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;
도움이 되었습니까?

해결책

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.

다른 팁

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top