How do I change a sql server password that has expired from C# code?

后端 未结 1 1329
半阙折子戏
半阙折子戏 2021-01-19 12:43

When you are using a SqlConnection to connect to a MS Sql Server, if the password has expired you will get a SqlException Number: 18487 or 18488.

How can you change

相关标签:
1条回答
  • 2021-01-19 13:22

    Use the static SqlConnection.ChangePassword() method.

    string original_dsn = "server=mysql.server.com,1433;database=pubdb;User Id={0};Password={1};"
    string dsn = String.Format(original_dsn, username, password);
    
    SqlConnection conn = new SqlConnection( dsn );
    try
    {
         conn.Open();
    }
    catch(SqlException e)
    {
        if (e.Number == 18487 || e.Number == 18488)
               SqlConnection.ChangePassword(dsn, newpassword);
               // Try login again here with new password
        else
               MessageBox.Show(e.Message);
    }
    finally 
    {
        conn.Close(); 
    }
    
    0 讨论(0)
提交回复
热议问题