다음을 통해 공유


OleDbCommand.Prepare 메서드

정의

데이터 원본에 준비된(또는 컴파일된) 버전의 명령을 만듭니다.

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 ()

구현

예외

Connection 설정되지 않았습니다.

-또는-

Connection 열려 있지 않습니다.

예제

다음 예제에서는 OleDbCommand 연결을 만들고 엽니다. 그런 다음, SQL SELECT 문인 문자열과 데이터 원본에 연결하는 데 사용할 문자열을 전달하여 데이터 원본에 저장 프로시저를 준비합니다.

private static void OleDbCommandPrepare(string connectionString)
{
    using (OleDbConnection connection = new
               OleDbConnection(connectionString))
    {
        connection.Open();

        // Create the Command.
        OleDbCommand command = new OleDbCommand();

        // Set the Connection, CommandText and Parameters.
        command.Connection = connection;
        command.CommandText =
            "INSERT INTO dbo.Region (RegionID, RegionDescription) VALUES (?, ?)";
        command.Parameters.Add("RegionID", OleDbType.Integer, 4);
        command.Parameters.Add("RegionDescription", OleDbType.VarWChar, 50);
        command.Parameters[0].Value = 20;
        command.Parameters[1].Value = "First Region";

        // Call  Prepare and ExecuteNonQuery.
        command.Prepare();
        command.ExecuteNonQuery();

        // Change parameter values and call ExecuteNonQuery.
        command.Parameters[0].Value = 21;
        command.Parameters[1].Value = "SecondRegion";
        command.ExecuteNonQuery();
    }
}
Public Sub OleDbCommandPrepare(ByVal connectionString As String)

    Using connection As OleDbConnection = New _
        OleDbConnection(connectionString)
        connection.Open()

        ' Create the Command.
        Dim command As New OleDbCommand()

        ' Set the Connection, CommandText and Parameters.
        command.Connection = connection
        command.CommandText = _
          "INSERT INTO dbo.Region (RegionID, RegionDescription) VALUES (?, ?);"
        command.Parameters.Add("RegionID", OleDbType.Integer, 4)
        command.Parameters.Add("RegionDescription", OleDbType.VarWChar, 50)
        command.Parameters(0).Value = 20
        command.Parameters(1).Value = "First Region"

        ' Call  Prepare and ExecuteNonQuery.
        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

설명

속성이 CommandType 설정된 TableDirectPrepare 경우 아무 것도 수행하지 않습니다. 설정된 StoredProcedure경우 CommandType 호출이 성공해야 Prepare 하지만 no-op발생할 수 있습니다.

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

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

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

적용 대상

추가 정보