ExecuteScalar、ExecuteReader 和 ExecuteNonQuery 之间有什么区别?

发布于 2024-09-03 20:10:13 字数 49 浏览 2 评论 0原文

当我们使用这三个的时候,有什么不同的情况呢?我应该在哪里使用它,在哪里不应该使用它?

What are the different cases when we use these three? Where should I use one and where should I not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

甜尕妞 2024-09-10 20:10:13
  • ExecuteScalar 通常在查询返回单个值时使用。如果返回 more,则结果是第一行的第一列。例如,SELECT @@IDENTITY AS 'Identity'
  • ExecuteReader 用于任何具有多行/列的结果集(例如,SELECT col1, col2 from sometable)。
  • ExecuteNonQuery 通常用于没有结果的 SQL 语句(例如 UPDATE、INSERT 等)。
  • ExecuteScalar is typically used when your query returns a single value. If it returns more, then the result is the first column of the first row. An example might be SELECT @@IDENTITY AS 'Identity'.
  • ExecuteReader is used for any result set with multiple rows/columns (e.g., SELECT col1, col2 from sometable).
  • ExecuteNonQuery is typically used for SQL statements without results (e.g., UPDATE, INSERT, etc.).
迟月 2024-09-10 20:10:13

ExecuteNonQuery():

  1. 仅适用于操作查询(创建、更改、删​​除、插入、更新、删除)。
  2. 返回受查询影响的行数。
  3. 返回类型为 int
  4. 返回值是可选的并且可以分配给整型变量。

ExecuteReader():

  1. 适用于操作和非操作查询(选择)
  2. 返回查询选择的行的集合。
  3. 返回类型是DataReader。
  4. 返回值是强制性的,应分配给另一个对象 DataReader。

ExecuteScalar():

  1. 将与包含聚合函数的非操作查询一起使用。
  2. 返回查询结果的第一行第一列值。
  3. 返回类型是对象。
  4. 返回值是强制性的,并且应该分配给所需类型的变量。

参考网址:

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

ExecuteNonQuery():

  1. will work with Action Queries only (Create,Alter,Drop,Insert,Update,Delete).
  2. Returns the count of rows effected by the Query.
  3. Return type is int
  4. Return value is optional and can be assigned to an integer variable.

ExecuteReader():

  1. will work with Action and Non-Action Queries (Select)
  2. Returns the collection of rows selected by the Query.
  3. Return type is DataReader.
  4. Return value is compulsory and should be assigned to an another object DataReader.

ExecuteScalar():

  1. will work with Non-Action Queries that contain aggregate functions.
  2. Return the first row and first column value of the query result.
  3. Return type is object.
  4. Return value is compulsory and should be assigned to a variable of required type.

Reference URL:

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

ˉ厌 2024-09-10 20:10:13

每一个都是不同类型的执行。

  • ExecuteScalar 将是类型
    的查询将返回
    单一值。

    一个示例是在插入后返回生成的 ID。

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

  • ExecuteReader 为您提供数据读取器
    返回,您可以阅读全部内容
    结果的列数为一行
    一次。

    一个示例是提取一个或多个用户的个人资料信息。

    SELECT * FROM my_profile WHERE id = '123456'

  • ExecuteNonQuery 是任意SQL其中
    不是返回值,而是
    实际上执行某种形式的工作
    比如插入删除或者修改
    某物。

    一个示例是更新数据库中的用户个人资料。

    更新 my_profile SET 地址 = '123 Fake St.' WHERE id = '123456'

Each one is a different type execution.

  • ExecuteScalar is going to be the type
    of query which will be returning a
    single value.

    An example would be returning a generated id after inserting.

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

  • ExecuteReader gives you a data reader
    back which will allow you to read all
    of the columns of the results a row
    at a time.

    An example would be pulling profile information for one or more users.

    SELECT * FROM my_profile WHERE id = '123456'

  • ExecuteNonQuery is any SQL which
    isn't returning values, but is
    actually performing some form of work
    like inserting deleting or modifying
    something.

    An example would be updating a user's profile in the database.

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

向地狱狂奔 2024-09-10 20:10:13

来自文档(注意:当您想知道事物的作用时,MSDN 是一个方便的资源!):

执行标量

使用 ExecuteScalar 方法从数据库检索单个值(例如聚合值)。与使用 ExecuteReader 方法,然后使用 SqlDataReader 返回的数据执行生成单个值所需的操作相比,这需要更少的代码。

ExecuteReader

将 CommandText 发送到连接并构建 SqlDataReader。

...以及来自 SqlDataReader ...

提供一种从 SQL Server 数据库读取只进行流的方法。该类不能被继承。

ExecuteNonQuery

您可以使用 ExecuteNonQuery 执行目录操作(例如,查询数据库的结构或创建表等数据库对象),或者通过执行 UPDATE、INSERT 或删除语句。

From the docs (note: MSDN is a handy resource when you want to know what things do!):

ExecuteScalar

Use the ExecuteScalar method to retrieve a single value (for example, an aggregate value) from a database. This requires less code than using the ExecuteReader method, and then performing the operations that you need to generate the single value using the data returned by a SqlDataReader.

ExecuteReader

Sends the CommandText to the Connection and builds a SqlDataReader.

... and from SqlDataReader ...

Provides a way of reading a forward-only stream of rows from a SQL Server database. This class cannot be inherited.

ExecuteNonQuery

You can use the ExecuteNonQuery to perform catalog operations (for example, querying the structure of a database or creating database objects such as tables), or to change the data in a database without using a DataSet by executing UPDATE, INSERT, or DELETE statements.

能否归途做我良人 2024-09-10 20:10:13

添加到其他人发布的内容:

ExecuteScalar 从概念上讲返回查询结果集第一行的最左边的列;您可以 ExecuteScalar a SELECT * FROM Staff,但您只能获得结果行的第一个单元格
通常用于返回单个值的查询。我对 SQLServer 不是 100% 确定,但在 Oracle 中,您不会使用它来运行 FUNCTION(返回单个值的数据库代码)并期望它为您提供函数的返回值即使函数返回单个值。但是,如果您将函数作为查询的一部分运行,例如 SELECT SUBSTR('abc', 1, 1) FROM DUAL 那么它将给出返回值,因为返回值存储在结果行集最左上角的单元格中。

ExecuteNonQuery 将用于运行修改数据(INSERT/UPDATE/DELETE)或修改数据库结构(CREATE TABLE...)的数据库存储过程、函数和查询。通常,调用的返回值指示有多少行受到该操作的影响,但请检查数据库文档以保证这一点

To add to what others posted:

ExecuteScalar conceptually returns the leftmost column from the first row of the resultset from the query; you could ExecuteScalar a SELECT * FROM staff, but you'd only get the first cell of the resulting rows
Typically used for queries that return a single value. I'm not 100% sure about SQLServer but in Oracle, you wouldnt use it to run a FUNCTION (a database code that returns a single value) and expect it to give you the return value of the function even though functions return single values.. However, if youre running the function as part of a query, e.g. SELECT SUBSTR('abc', 1, 1) FROM DUAL then it would give the return value by virtue of the fact that the return value is stored in the top leftmost cell of the resulting rowset

ExecuteNonQuery would be used to run database stored procedures, functions and queries that modify data (INSERT/UPDATE/DELETE) or modify database structure (CREATE TABLE...). Typically the return value of the call is an indication of how many rows were affected by the operation but check the DB documentation to guarantee this

旧人 2024-09-10 20:10:13

ExecuteReader() 执行 SQL 查询,该查询返回数据提供程序 DBDataReader 对象,该对象为查询结果提供仅前向和只读访问。

ExecuteScalar() 类似于 ExecuteReader() 方法,该方法专为单例查询(例如获取记录计数)而设计。

ExecuteNonQuery() 执行与创建、删除、更新、插入一起使用的非查询)

ExecuteReader() executes a SQL query that returns the data provider DBDataReader object that provide forward only and read only access for the result of the query.

ExecuteScalar() is similar to ExecuteReader() method that is designed for singleton query such as obtaining a record count.

ExecuteNonQuery() execute non query that works with create ,delete,update, insert)

Bonjour°[大白 2024-09-10 20:10:13

ExecuteNonQuery

此 ExecuteNonQuery 方法仅用于插入、更新和删除、Create 和 SET 语句。
ExecuteNonQuery 方法将返回受 INSERT、DELETE 或 UPDATE 操作影响的行数。

ExecuteScalar

从数据库中检索单个值的速度非常快。
在使用命令对象执行 SQL 查询或存储过程时,执行标量将返回单行单列值,即单个值。
ExecuteReader

执行读取器将用于在使用命令对象执行 SQL 查询或存储过程时返回行集。这是仅向前检索记录,用于从第一个到最后一个读取表值。

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.

岁月流歌 2024-09-10 20:10:13

ExecuteNonQuery 方法将返回受 INSERT、DELETE 或 UPDATE 操作影响的行数。此 ExecuteNonQuery 方法仅用于插入、更新和删除、Create 和 SET 语句。 (了解更多)

ExecuteScalar 将返回单行单列值,即使用命令对象执行 SQL 查询或存储过程时的单值。从数据库中检索单个值的速度非常快。 (了解更多)

将用于在使用命令对象执行 SQL 查询或存储过程时返回行集。这是仅向前检索记录,用于从第一个到最后一个读取表值。 (了解更多)

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)

暮年 2024-09-10 20:10:13

ExecuteNonQuery:通常在插入、更新、删除操作等 Sql 语句没有返回任何内容时使用。

cmd.ExcecuteNonQuery();

ExecuteScalar:

当Sql查询返回单个值时将使用它。

Int b = cmd.ExcecuteScalar();

ExecuteReader

当Sql查询或存储过程返回多行/列时将使用它,

SqlDataReader dr = cmd.ExecuteReader();

有关更多信息,您可以点击这里http://www.dotnetqueries.com/Article/148/-difference- Between-executescalar-executereader-executenonquery

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

风柔一江水 2024-09-10 20:10:13

ExecuteNonQuery - 如果您不希望返回任何结果集,请使用此操作在 SQL Server 中执行任何任意 SQL 语句。您可以使用此操作来创建数据库对象或更改
通过执行 UPDATE、INSERT 或 DELETE 语句将数据存储到数据库中。

ExecuteScalar- ExecuteScalar() 执行 SQL 语句以及存储过程,并在返回的结果集中第一行的第一列返回标量值。与 Count(*) 或 Sum() 等聚合函数一起使用

ExecuteReader 方法 非常有用用于执行 SQL 命令或存储过程从数据库返回一组行。

ExecuteNonQuery - Use this operation to execute any arbitrary SQL statements in SQL Server if you do not want any result set to be returned. You can use this operation to create database objects or change
data in a database by executing UPDATE, INSERT, or DELETE statements.

ExecuteScalar- ExecuteScalar() executes SQL statements as well as Stored Procedure and returned a scalar value on first column of first row in the returned Result Set.It is very useful to use with aggregate functions like Count(*) or Sum()

ExecuteReader method is used to execute a SQL Command or storedprocedure returns a set of rows from the database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文