0

I'm trying to change passwords of SQL server logins, but the Passwords just won't change. This is what I have:

SqlCommand query = new SqlCommand("ALTER LOGIN @login WITH PASSWORD = '@Pwd'", con);
query.Parameters.Add("@login", SqlDbType.NVarChar, 60).Value = login;
query.Parameters.Add("@Pwd", SqlDbType.NVarChar, 20).Value = _newPwds[login];
query.Prepare();
query.BeginExecuteNonQuery();

I don't get any error messages back from the SQL Server or debugging (Or I don't know how to get them). The Connection is correct and I have the rights to Change Passwords on the Server (Application is running with Integrated Security).

Thanks for your help.

mason
  • 31,774
  • 10
  • 77
  • 121
  • @mason Thanks, didn't know that. I changed it and now I get "Incorrect Syntax near '@login'". – M.Zuberbühler Sep 18 '17 at 15:43
  • See [this question](https://stackoverflow.com/questions/14858034/how-to-change-a-sql-login-password-with-variables) for an example of using parameters with an alter login statement. – mason Sep 18 '17 at 15:48
  • @mason Thanks! I'll test that on Thursday, but I think this will do. Could you add this to your answer? Then I would mark it as the right answer after I tested it. – M.Zuberbühler Sep 18 '17 at 16:20

1 Answers1

1

You are using an asynchronous method BeginExecuteNonQuery to execute the query, so you haven't captured it. Given you probably didn't want to go async, try using ExecuteNonQuery instead:

query.ExecuteNonQuery();

Plus you can't use parameters in an ALTER LOGIN statement, see this question.

mason
  • 31,774
  • 10
  • 77
  • 121