프로그래밍 기술/SQL과 ADO.NET

[ADO.NET] SqlCommand 생성자, 속성

언제나휴일 2016. 4. 22. 12:26
반응형

SqlCommand 생성자

 

 SqlCommand 개체는 SqlConnection 개체의 CreateCommand 메서드를 이용하거나 SqlCommand 생성자 호출로 생성할 수 있습니다. 다음은 두 가지 방법을 사용한 예입니다.(예외처리 구문 생략하였음)

 


Program.cs


class Program

{

    static void Main(string[] args)

    {

        string constr = @"Data Source=[서버 이름];Initial Catalog=[DB ]; User ID=[ID];Password=[PW]";

        string comtxt = "insert into Product (PNAME, Price, Description) values ('ADO.NET',20000,'생략')";

        ExCreateCommand(constr,comtxt);

        ExSqlCommandCreator(constr, comtxt);

    }

    private static void ExSqlCommandCreator(string constr,string comtxt)

    {

        SqlConnection scon = new SqlConnection(constr);

        SqlCommand scom = scon.CreateCommand();

        scom.CommandText = comtxt;

        ExecCommand(scon,scom);

    }

    private static void ExCreateCommand(string constr, string comtxt)

    {

        SqlConnection scon = new SqlConnection(constr);

        SqlCommand scom = new SqlCommand(comtxt, scon);

        ExecCommand(scon, scom);

    }

    private static void ExecCommand(SqlConnection scon,SqlCommand scom)

    {

        scon.Open();

        if (scom.ExecuteNonQuery() == 1)

        {

            Console.WriteLine("추가 성공");

        }

        scon.Close();

    }

}

 

 SqlCommand 클래스에는 네 가지 생성자를 제공합니다.

 

SqlCommand();

SqlCommand(string comText);

SqlCommand(string comText, SqlConnection scon);

SqlCommand(string comText, SqlConnection scon, SqlTransaction stran);

 

SqlCommand();

 입력인자를 전달하지 않고 SqlCommand 개체를 생성하였을 때는 별도의 구문으로 SqlConnection 개체와 실제 수행할 명령을 설정한 후에 명령을 실행시킬 수 있습니다.

 

속성

초기값

설명

CommandText

string.Empty

수행할 명령

CommandType

CommandType.Text

명령 종류

Connection

null

데이터 소스와 연결 SqlConnection 개체

 

SqlCommand(string comText);

 SqlCommand 개체를 생성할 때 실제 수행할 명령을 인자로 전달하여 생성할 수 있으며 이 때 명령 종류를 의미하는 CommandType 속성은 CommadType.Text입니다. CommandType.Text SQL 쿼리문을 의미하며 생성할 때 전달한 명령이 저장 프로시저 이름이라면 CommandType.StoredProcedure로 설정해야 합니다.

 

 SqlCommand 개체를 이용하여 작업을 수행할 때 작업 형식으로 SQL 명령어 혹은 저장 프로시저 이름을 사용할 수 있습니다. OLE DB일 때는 TableDirect도 사용할 수 있지만 SqlCommand를 이용할 때는 사용할 수 없습니다. SqlCommand 개체의 디폴트 작업 형식은 SQL 명령어로 되어 있는데 만약 저장 프로시저를 사용하려면 CommandType 속성을 CommandType 열거형의 StoredProcedure로 설정하세요.

 

scom.CommandType = CommandType.StoredProcedure;

 

 SqlCommand에 매개 변수를 설정하여 작업을 수행할 때는 SqlParameter 개체를 사용하고 작업의 종류에 따라 실행할 수 있는 다양한 메서드를 제공하고 있는데 이들은 SqlParameter SqlCommand의 실행 메서드들에서 다룰게요.

 

SqlCommand(string comText, SqlConnection scon);

 SqlCommand  개체를 이용하여 실제 명령을 수행하기 위해선 반드시 SqlConnection 개체가 설정되어 있어야 합니다. SqlCommand 클래스의 생성자 중에는 SqlConnection 개체를 입력 인자로 받는 생성자를 제공하고 있습니다.

 

SqlCommand(string comText, SqlConnection scon, SqlTransaction stran);

 DB 관련 명령을 수행할 때는 여러 개의 명령을 하나의 논리 작업으로 묶어서 작업 중에 문제가 있을 때 작업 전 상태로 복원할 수 있습니다. 이와 같이 처리하려면 여러 개의 명령을 하나의 논리 작업으로 묶는 개체가 필요한데 SqlTransaction으로 제공하고 있습니다


SqlCommand 속성

 

 SqlCommand 개체는 명령에 관한 여러 가지 속성을 제공합니다. 다음은 주요한 속성입니다.

 

속성

가져오기/설정하기

설명

CommandText

가져오기/설정하기

실행할 SQL문이나 테이블 이름,

저장 프로시저 이름

CommandTimeout

가져오기/설정하기

명령 실행할 때 대기시간(,기본값 30)

CommandType

가져오기/설정하기

명령 종류

DbConnection

가져오기/설정하기

명령에 사용할 DbConnection 개체

Parameters

가져오기

SqlParameterColletion

Transaction

가져오기/설정하기

명령을 실행할 때 SqlTransaction 개체

 

 CommandText 속성은 실제 수행할 명령에 관한 속성입니다. CommadText 속성 값은 CommandType 속성이 CommandType.Text일 때는 명령에 수행할 SQL문을 설정합니다. 그리고 CommandText.StoredProcedure일 때는 저장 프로서저 이름으로 설정합니다.

 

 만약 명령에 매개 변수를 사용하고자 한다면 Parameters 속성에 SqlParameter 개체를 만들어 추가하세요.

 

SqlCommand command = new SqlCommand();

command.CommandText = "SELECT * FROM Books ";

command.CommandTimeout = 15;

command.CommandType = CommandType.Text;

반응형