SqlCommand.Prepare 메서드

정의

SQL Server 인스턴스의 명령에 사용할 버전을 만듭니다.

public:
 override void Prepare();
public:
 virtual void Prepare();
public override void Prepare ();
public void Prepare ();
override this.Prepare : unit -> unit
abstract member Prepare : unit -> unit
override this.Prepare : unit -> unit
Public Overrides Sub Prepare ()
Public Sub Prepare ()

구현

예제

다음 예제에서는 Prepare 메서드를 사용하는 방법을 보여 줍니다.

private static void SqlCommandPrepareEx(string connectionString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        SqlCommand command = new SqlCommand(null, connection);

        // Create and prepare an SQL statement.
        command.CommandText =
            "INSERT INTO Region (RegionID, RegionDescription) " +
            "VALUES (@id, @desc)";
        SqlParameter idParam = new SqlParameter("@id", SqlDbType.Int, 0);
        SqlParameter descParam =
            new SqlParameter("@desc", SqlDbType.Text, 100);
        idParam.Value = 20;
        descParam.Value = "First Region";
        command.Parameters.Add(idParam);
        command.Parameters.Add(descParam);

        // Call Prepare after setting the Commandtext and Parameters.
        command.Prepare();
        command.ExecuteNonQuery();

        // Change parameter values and call ExecuteNonQuery.
        command.Parameters[0].Value = 21;
        command.Parameters[1].Value = "Second Region";
        command.ExecuteNonQuery();
    }
}
Private Sub SqlCommandPrepareEx(ByVal connectionString As String)
    Using connection As New SqlConnection(connectionString)
        connection.Open()
        Dim command As New SqlCommand("", connection)

        ' Create and prepare an SQL statement.
        command.CommandText = _
           "INSERT INTO Region (RegionID, RegionDescription) " & _
           "VALUES (@id, @desc)"
        Dim idParam As SqlParameter = _
            New SqlParameter("@id", SqlDbType.Int, 0)
        Dim descParam As SqlParameter = _
            New SqlParameter("@desc", SqlDbType.Text, 100)
        idParam.Value = 20
        descParam.Value = "First Region"
        command.Parameters.Add(idParam)
        command.Parameters.Add(descParam)

        ' Call Prepare after setting the Commandtext and Parameters.
        command.Prepare()
        command.ExecuteNonQuery()

        ' Change parameter values and call ExecuteNonQuery.
        command.Parameters(0).Value = 21
        command.Parameters(1).Value = "Second Region"
        command.ExecuteNonQuery()
    End Using
End Sub

설명

가 로 StoredProcedure설정된 경우 CommandType 에 대한 Prepare 호출은 성공해야 하지만, 이 호출로 인해 작업이 중단될 수 있습니다.

를 호출 Prepare하기 전에 준비할 문에서 각 매개 변수의 데이터 형식을 지정합니다. 변수 길이 데이터 형식이 있는 각 매개 변수에 Size 대해 속성을 필요한 최대 크기로 설정해야 합니다. Prepare 는 이러한 조건이 충족되지 않으면 오류를 반환합니다.

참고

Transact-SQL USE <database> 문을 실행하거나 메서드 Prepare 를 호출하여 데이터베이스 컨텍스트를 ChangeDatabase 변경하는 경우 를 두 번째로 호출해야 합니다.

를 호출한 후 메서드를 Execute 호출 Prepare하는 경우 속성에 지정된 Size 값보다 큰 매개 변수 값이 매개 변수의 원래 지정된 크기로 자동으로 잘리고 잘림 오류가 반환되지 않습니다.

출력 매개 변수(준비 여부)에는 사용자 지정 데이터 형식이 있어야 합니다. 가변 길이 데이터 형식을 지정하는 경우 최대 Size을 지정해야 합니다.

Visual Studio 2010 Prepare 이전에는 예외가 발생했습니다. Visual Studio 2010부터 이 메서드는 예외를 throw하지 않습니다.

적용 대상

추가 정보