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

两盒软妹~` 提交于 2019-12-01 19:52:48

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(); 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!