ExecuteScalar、ExecuteReader 和 ExecuteNonQuery 之间有什么区别?
当我们使用这三个的时候,有什么不同的情况呢?我应该在哪里使用它,在哪里不应该使用它?
What are the different cases when we use these three? Where should I use one and where should I not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
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 beSELECT @@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.).ExecuteNonQuery():
ExecuteReader():
ExecuteScalar():
参考网址:
http://nareshkamuni.blogspot.in/ 2012/05/what-is-difference- Between.html
ExecuteNonQuery():
ExecuteReader():
ExecuteScalar():
Reference URL:
http://nareshkamuni.blogspot.in/2012/05/what-is-difference-between.html
每一个都是不同类型的执行。
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'
来自文档(注意:当您想知道事物的作用时,MSDN 是一个方便的资源!):
执行标量
ExecuteReader
...以及来自 SqlDataReader ...
ExecuteNonQuery
From the docs (note: MSDN is a handy resource when you want to know what things do!):
ExecuteScalar
ExecuteReader
... and from SqlDataReader ...
ExecuteNonQuery
添加到其他人发布的内容:
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
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 toExecuteReader()
method that is designed for singleton query such as obtaining a record count.ExecuteNonQuery()
execute non query that works with create ,delete,update, insert)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.
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)
ExecuteNonQuery:通常在插入、更新、删除操作等 Sql 语句没有返回任何内容时使用。
ExecuteScalar:
当Sql查询返回单个值时将使用它。
ExecuteReader
当Sql查询或存储过程返回多行/列时将使用它,
有关更多信息,您可以点击这里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.
ExecuteScalar:
It will be used when Sql query returns single value.
ExecuteReader
It will be used when Sql query or Stored Procedure returns multiple rows/columns
for more information you can click here http://www.dotnetqueries.com/Article/148/-difference-between-executescalar-executereader-executenonquery
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.