当您使用 SqlConnection 连接到 MS Sql Server 时,如果密码已过期,您将收到 SqlException 编号:18487 或 18488。
如何在连接尝试期间更改代码中的用户密码?
使用静态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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)