Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

SqlConnection::ChangePassword Method (String^, String^)

 

Changes the SQL Server password for the user indicated in the connection string to the supplied new password.

Namespace:   System.Data.SqlClient
Assembly:  System.Data (in System.Data.dll)

public:
static void ChangePassword(
	String^ connectionString,
	String^ newPassword
)

Parameters

connectionString
Type: System::String^

The connection string that contains enough information to connect to the server that you want. The connection string must contain the user ID and the current password.

newPassword
Type: System::String^

The new password to set. This password must comply with any password security policy set on the server, including minimum length, requirements for specific characters, and so on.

Exception Condition
ArgumentException

The connection string includes the option to use integrated security.

Or

The newPassword exceeds 128 characters.

ArgumentNullException

Either the connectionString or the newPassword parameter is null.

When you are using SQL Server on Windows Server, developers can take advantage of functionality that lets the client application supply both the current and a new password in order to change the existing password. Applications can implement functionality such as prompting the user for a new password during initial login if the old one has expired, and this operation can be completed without administrator intervention.

The ChangePassword method changes the SQL Server password for the user indicated in the supplied connectionString parameter to the value supplied in the newPassword parameter. If the connection string includes the option for integrated security (that is, "Integrated Security=True" or the equivalent), an exception is thrown.

To determine that the password has expired, calling the Open method raises a SqlException. In order to indicate that the password that is contained within the connection string must be reset, the Number property for the exception contains the status value 18487 or 18488. The first value (18487) indicates that the password has expired and the second (18488) indicates that the password must be reset before logging in.

This method opens its own connection to the server, requests the password change, and closes the connection as soon as it has completed. This connection is not retrieved from, nor returned to, the SQL Server connection pool.

The following is a simple example of changing a password:

class Program {
   static void Main(string[] args) {
      System.Data.SqlClient.SqlConnection.ChangePassword(
        "Data Source=a_server;Initial Catalog=a_database;UID=user;PWD=old_password", 
       "new_password");
   }
}

Module Module1
    Sub Main()
System.Data.SqlClient.SqlConnection.ChangePassword(
        "Data Source=a_server;Initial Catalog=a_database;UID=user;PWD=old_password", 
       "new_password")
    End Sub
End Module

The following console application demonstrates the issues involved in changing a user's password because the current password has expired.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 2.0
Return to top
Show:
© 2017 Microsoft