SqlConnection.ChangePassword メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > SqlConnection.ChangePassword メソッドの意味・解説 

SqlConnection.ChangePassword メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

接続文字列中に指定されているユーザーSQL Server パスワードを、指定され新しパスワード変更します

名前空間: System.Data.SqlClient
アセンブリ: System.Data (system.data.dll 内)
構文構文

Public Shared Sub ChangePassword
 ( _
    connectionString As String, _
    newPassword As String _
)
Dim connectionString As String
Dim newPassword As String

SqlConnection.ChangePassword(connectionString, newPassword)
public static void ChangePassword
 (
    string connectionString,
    string newPassword
)
public:
static void ChangePassword (
    String^ connectionString, 
    String^ newPassword
)
public static void ChangePassword
 (
    String connectionString, 
    String newPassword
)
public static function ChangePassword
 (
    connectionString : String, 
    newPassword : String
)

パラメータ

connectionString

目的サーバー接続するために必要な情報保持する接続文字列接続文字列には、ユーザー ID および現在のパスワード含まれている必要があります

newPassword

新たに設定するパスワード。このパスワードは、サーバー側で設定されているパスワード セキュリティ ポリシー (最低限長さ使用文字要件など) を満たしている必要があります

例外例外
例外種類条件

ArgumentException

接続文字列に、統合セキュリティ使用するためのオプション含まれます。

ArgumentNullException

connectionString パラメータまたは newPassword パラメータnull です。

解説解説

Windows Server 2003 以降SQL Server 2005使用している場合現在のパスワード新しパスワード両方指定して既存パスワード変更できるようにする機能クライアント アプリケーション実装できます。たとえば、最初ログイン時に、古いパスワード期限切れていた場合管理者介入することなくユーザー新しパスワード入力求めることもできます

ChangePassword は、connectionString パラメータ指定されユーザーSQL Server パスワードを、newPassword パラメータ指定された値に変更できるようにするメソッドです。接続文字列統合セキュリティ用のオプション ("Integrated Security=True" など) が含まれていた場合例外スローさます。

パスワード有効期限切れているかどうかは、Open メソッド使用し、SqlException を発生させることによって判断できます接続文字列中のパスワードリセットする必要がある場合は、Number プロパティに 18487 または 18488 のステータス値格納されます。ここで、18487 はパスワード有効期限切れていることを示し、18488 はパスワードリセットしてからログインする必要があることを示します

このメソッドでは、サーバー対す専用接続確立しパスワード変更要求して変更完了した段階接続解除されます。この接続SQL Server接続プールか取得されたり、逆に接続プール戻されたりすることはありません。

使用例使用例

次のコンソール アプリケーションでは、現在のパスワード有効期限切れている場合に、ユーザーパスワード変更求めます

Option Explicit On
Option Strict On

Imports System
Imports System.Data
Imports System.Data.SqlClient

Module Module1
    Sub Main()
        Try
            DemonstrateChangePassword()
        Catch ex As Exception
            Console.WriteLine("Error: " & ex.Message)
        End Try
        Console.WriteLine("Press ENTER to continue...")
        Console.ReadLine()
    End Sub

    Private Sub DemonstrateChangePassword()
        Dim connectionString As String
 = GetConnectionString()
        Using cnn As New SqlConnection()
            For i As Integer
 = 0 To 1
                ' Run this loop at most two times. If the first attempt
 fails, 
                ' the code checks the Number property of the SqlException
 object.
                ' If that contains the special values 18487 or 18488,
 the code 
                ' attempts to set the user's password to a new value.
 
                ' Assuming this succeeds, the second pass through 
                ' successfully opens the connection.
                ' If not, the exception handler catches the exception.
                Try
                    cnn.ConnectionString = connectionString
                    cnn.Open()
                    ' Once this succeeds, just get out of the loop.
                    ' No need to try again if the connection is already
 open.
                    Exit For

                Catch ex As SqlException _
                 When (i = 0 And (ex.Number
 = 18487 Or ex.Number = 18488))
                    ' You must reset the password.
                    connectionString = ModifyConnectionString( _
                     connectionString, GetNewPassword())

                Catch ex As SqlException
                    ' Bubble all other SqlException occurrences
                    ' back up to the caller.
                    Throw
                End Try
            Next
            Dim cmd As New
 SqlCommand("SELECT ProductID, Name FROM Product",
 cnn)
            ' Use the connection and command here...
        End Using
    End Sub

    Private Function ModifyConnectionString(
 _
     ByVal connectionString As String,
 ByVal NewPassword As String)
 As String

        ' Use the SqlConnectionStringBuilder class to modify the
        ' password portion of the connection string. 
        Dim builder As New
 SqlConnectionStringBuilder(connectionString)
        builder.Password = NewPassword
        Return builder.ConnectionString
    End Function

    Private Function GetNewPassword() As
 String
        ' In a real application, you might display a modal
        ' dialog box to retrieve the new password. The concepts
        ' are the same as for this simple console application, however.
        Console.Write("Your password must be reset. Enter a new
 password: ")
        Return Console.ReadLine()
    End Function

    Private Function GetConnectionString()
 As String
        ' For this demonstration, the connection string must
        ' contain both user and password information. In your own
        ' application, you might want to retrieve this setting
        ' from a config file, or from some other source.

        ' In a production application, you would want to 
        ' display a modal form that could gather user and password
        ' information.
        Dim builder As New
 SqlConnectionStringBuilder( _
         "Data Source=(local);Initial Catalog=AdventureWorks")

        Console.Write("Enter your user id: ")
        builder.UserID = Console.ReadLine()
        Console.Write("Enter your password: ")
        builder.Password = Console.ReadLine()

        Return builder.ConnectionString
    End Function
End Module
using System;
using System.Data;
using System.Data.SqlClient;

class Program
{
    static void Main()
    {
        try
        {
            DemonstrateChangePassword();
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
        Console.WriteLine("Press ENTER to continue...");
        Console.ReadLine();
    }

    private static void
 DemonstrateChangePassword()
    {
        // Retrieve the connection string. In a production application
,
        // this string should not be contained within the source code.
        string connectionString = GetConnectionString();

        using (SqlConnection cnn = new SqlConnection())
        {
            for (int i = 0; i <= 1; i++)
            {
                // Run this loop at most two times. If the first attempt
 fails, 
                // the code checks the Number property of the SqlException
 object.
                // If that contains the special values 18487 or 18488,
 the code 
                // attempts to set the user's password to a new value.
 
                // Assuming this succeeds, the second pass through 
                // successfully opens the connection.
                // If not, the exception handler catches the exception.
                try
                {
                    cnn.ConnectionString = connectionString;
                    cnn.Open();
                    // Once this succeeds, just get out of the loop.
                    // No need to try again if the connection is already
 open.
                    break;
                }
                catch (SqlException ex)
                {
                    if (i == 0 && ((ex.Number == 18487)
 || (ex.Number == 18488)))
                    {
                        // You must reset the password. 
                        connectionString =
                            ModifyConnectionString(connectionString, 
                            GetNewPassword());

                    }
                    else
                        // Bubble all other SqlException occurrences
                        // back up to the caller.
                        throw;
                }
            }
            SqlCommand cmd = new SqlCommand(
                "SELECT ProductID, Name FROM Product", cnn);
            // Use the connection and command here...
        }
    }

    private static string
 ModifyConnectionString(
        string connectionString, string NewPassword)
    {

        // Use the SqlConnectionStringBuilder class to modify the
        // password portion of the connection string. 
        SqlConnectionStringBuilder builder =
            new SqlConnectionStringBuilder(connectionString);
        builder.Password = NewPassword;
        return builder.ConnectionString;
    }

    private static string
 GetNewPassword()
    {
        // In a real application, you might display a modal
        // dialog box to retrieve the new password. The concepts
        // are the same as for this simple console application, however.
        Console.Write("Your password must be reset. Enter a new
 password: ");
        return Console.ReadLine();
    }

    private static string
 GetConnectionString()
    {
        // For this demonstration, the connection string must
        // contain both user and password information. In your own
        // application, you might want to retrieve this setting
        // from a config file, or from some other source.

        // In a production application, you would want to 
        // display a modal form that could gather user and password
        // information.
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(
            "Data Source=(local);Initial Catalog=AdventureWorks");

        Console.Write("Enter your user id: ");
        builder.UserID = Console.ReadLine();
        Console.Write("Enter your password: ");
        builder.Password = Console.ReadLine();

        return builder.ConnectionString;
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

SqlConnection.ChangePassword メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



SqlConnection.ChangePassword メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS