SqlCommand.Prepare 메서드

정의

SQL Server 인스턴스에서 준비된 버전의 명령을 만듭니다.

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

예제

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

using System;
using System.Data;
using Microsoft.Data.SqlClient;

namespace SqlCommand_Prepare
{
    class Program
    {
        static void Main()
        {
            string connectionString = "Persist Security Info=False;Integrated Security=SSPI;database=Northwind;server=(local)";
            SqlCommandPrepareEx(connectionString);
            Console.ReadLine();

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

설명

설정된 CommandType경우 StoredProcedure 호출이 성공해야 Prepare 하지만 no-op발생할 수 있습니다.

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

메모

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

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

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

벡터 데이터 형식의 Size 경우 속성이 무시됩니다. 벡터의 크기는 형식SqlVector<T>에서 유추됩니다Value.

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

적용 대상