code

ExecuteScalar, ExecuteReader 및 ExecuteNonQuery의 차이점은 무엇입니까?

codestyles 2020. 8. 21. 07:46
반응형

ExecuteScalar, ExecuteReader 및 ExecuteNonQuery의 차이점은 무엇입니까?


이 세 가지를 사용할 때 다른 경우는 무엇입니까? 어디에서 사용해야하나요?


  • ExecuteScalar일반적으로 쿼리가 단일 값을 반환 할 때 사용됩니다. 더 많은 것을 반환하면 결과는 첫 번째 행의 첫 번째 열입니다. 예를 들면 SELECT @@IDENTITY AS 'Identity'.
  • ExecuteReader행 / 열이 여러 개인 결과 집합에 사용됩니다 (예 :) SELECT col1, col2 from sometable.
  • ExecuteNonQuery 일반적으로 결과가없는 SQL 문에 사용됩니다 (예 : UPDATE, INSERT 등).

ExecuteNonQuery () :

  1. 작업 쿼리 (만들기, 변경, 놓기, 삽입, 업데이트, 삭제)에서만 작동합니다.
  2. 쿼리의 영향을받는 행 수를 반환합니다.
  3. 반환 유형은 int입니다.
  4. 반환 값은 선택 사항이며 정수 변수에 할당 할 수 있습니다.

ExecuteReader () :

  1. 액션 및 비 액션 쿼리에서 작동합니다 (선택).
  2. 쿼리에서 선택한 행 컬렉션을 반환합니다.
  3. 반환 유형은 DataReader입니다.
  4. 반환 값은 필수이며 다른 개체 DataReader에 할당되어야합니다.

ExecuteScalar () :

  1. 집계 함수를 포함하는 비 조치 쿼리와 함께 작동합니다.
  2. 쿼리 결과의 첫 번째 행과 첫 번째 열 값을 반환합니다.
  3. 반환 유형은 객체입니다.
  4. 반환 값은 필수이며 필요한 유형의 변수에 할당되어야합니다.

참조 URL :

http://nareshkamuni.blogspot.in/2012/05/what-is-difference-between.html


각각은 다른 유형의 실행입니다.

  • ExecuteScalar 는 단일 값을 반환하는 쿼리 유형이 될 것입니다.

    예를 들어 삽입 후 생성 된 ID를 반환합니다.

    INSERT INTO my_profile (Address) VALUES ('123 Fake St.'); SELECT CAST(scope_identity() AS int)

  • ExecuteReader 는 한 번에 한 행씩 결과의 모든 열을 읽을 수있는 데이터 판독기를 제공합니다.

    예를 들어 한 명 이상의 사용자에 대한 프로필 정보를 가져 오는 것입니다.

    SELECT * FROM my_profile WHERE id = '123456'

  • ExecuteNonQuery 는 값을 반환하지 않지만 실제로 삽입 삭제 또는 수정과 같은 작업을 수행하는 SQL입니다.

    예를 들어 데이터베이스에서 사용자의 프로필을 업데이트 할 수 있습니다.

    UPDATE my_profile SET Address = '123 Fake St.' WHERE id = '123456'


문서에서 (참고 : MSDN은 어떤 일을하는지 알고 싶을 때 유용한 리소스입니다!) :

ExecuteScalar

ExecuteScalar 메서드를 사용하여 데이터베이스에서 단일 값 (예 : 집계 값)을 검색합니다. 이렇게하려면 ExecuteReader 메서드를 사용한 다음 SqlDataReader에서 반환 된 데이터를 사용하여 단일 값을 생성하는 데 필요한 작업을 수행하는 것보다 적은 코드가 필요합니다.

ExecuteReader

CommandText를 Connection에 보내고 SqlDataReader를 빌드합니다.

... 그리고 SqlDataReader에서 ...

SQL Server 데이터베이스에서 앞으로 만 이동 가능한 행 스트림을 읽는 방법을 제공합니다. 이 클래스는 상속 될 수 없습니다.

ExecuteNonQuery

ExecuteNonQuery를 사용하여 카탈로그 작업 (예 : 데이터베이스 구조 쿼리 또는 테이블과 같은 데이터베이스 개체 만들기)을 수행하거나 UPDATE, INSERT 또는 DELETE 문을 실행하여 DataSet을 사용하지 않고 데이터베이스의 데이터를 변경할 수 있습니다.


다른 사람이 게시 한 내용을 추가하려면 :

ExecuteScalar는 개념적으로 쿼리 결과 집합의 첫 번째 행에서 가장 왼쪽 열을 반환합니다. SELECT * FROM 직원을 ExecuteScalar 할 수 있지만 결과 행의 첫 번째 셀만 가져옵니다. 일반적으로 단일 값을 반환하는 쿼리에 사용됩니다. 나는 SQLServer에 대해 100 % 확신하지만, 오라클, 당신은 wouldnt가 함수 (데이터베이스 코드를 반환하는 하나의 값이)를 실행하는 데 사용하고 당신에게 기능을 하나의 값을 반환하더라도 함수의 반환 값을 할 수 있기를 기대한다. 그러나 쿼리의 일부로 함수를 실행하는 경우 (예 : SELECT SUBSTR ( 'abc', 1, 1) FROM DUAL) 반환 값이 맨 왼쪽 상단에 저장되어 있기 때문에 반환 값을 제공합니다. 결과 행 집합의 셀

ExecuteNonQuery는 데이터를 수정 (INSERT / UPDATE / DELETE)하거나 데이터베이스 구조를 수정 (CREATE TABLE ...)하는 데이터베이스 저장 프로 시저, 함수 및 쿼리를 실행하는 데 사용됩니다. 일반적으로 호출의 반환 값은 작업의 영향을받은 행 수를 나타내지 만이를 보장하려면 DB 문서를 확인하십시오.


ExecuteReader() 쿼리 결과에 대해 전달 전용 및 읽기 전용 액세스를 제공하는 데이터 공급자 DBDataReader 개체를 반환하는 SQL 쿼리를 실행합니다.

ExecuteScalar()ExecuteReader()레코드 수를 얻는 것과 같은 단일 쿼리를 위해 설계된 방법 과 유사합니다 .

ExecuteNonQuery() 생성, 삭제, 업데이트, 삽입과 함께 작동하는 비 쿼리 실행)


ExecuteNonQuery

This ExecuteNonQuery method will be used only for insert, update and delete, Create, and SET statements. ExecuteNonQuery method will return number of rows effected with INSERT, DELETE or UPDATE operations.

ExecuteScalar

It’s very fast to retrieve single values from database. Execute Scalar will return single row single column value i.e. single value, on execution of SQL Query or Stored procedure using command object. ExecuteReader

Execute Reader will be used to return the set of rows, on execution of SQL Query or Stored procedure using command object. This one is forward only retrieval of records and it is used to read the table values from first to last.


ExecuteNonQuery method will return number of rows effected with INSERT, DELETE or UPDATE operations. This ExecuteNonQuery method will be used only for insert, update and delete, Create, and SET statements. (Read More)

ExecuteScalar will return single row single column value i.e. single value, on execution of SQL Query or Stored procedure using command object. It’s very fast to retrieve single values from database. (Read More)

ExecuteReader will be used to return the set of rows, on execution of SQL Query or Stored procedure using command object. This one is forward only retrieval of records and it is used to read the table values from first to last. (Read More)


ExecuteNonQuery: is typically used when there is nothing returned from the Sql statements like insert ,update, delete operations.

cmd.ExcecuteNonQuery();

ExecuteScalar:

It will be used when Sql query returns single value.

Int b = cmd.ExcecuteScalar();

ExecuteReader

It will be used when Sql query or Stored Procedure returns multiple rows/columns

SqlDataReader dr = cmd.ExecuteReader();

for more information you can click here http://www.dotnetqueries.com/Article/148/-difference-between-executescalar-executereader-executenonquery

참고URL : https://stackoverflow.com/questions/2974154/what-is-the-difference-between-executescalar-executereader-and-executenonquery

반응형