asp.net中executecalar和executenonquery以及executereader的返回类型是什么 [英] what is the return type of executescalar and executenonquery and executereader in asp.net

查看:139
本文介绍了asp.net中executecalar和executenonquery以及executereader的返回类型是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能告诉我执行标量和执行读者的返回类型并执行非查询

Can you tell me the return types for execute scalar and execute reader and execute non query

推荐答案

ExecuteNonQuery: 使用如果您不想返回任何结果集,则执行此操作 SQL Server 中的任意SQL语句。



您可以使用此操作通过执行UPDATE,INSERT或DELETE语句来创建数据库对象或更改数据库中的数据。此操作的返回值是Int32数据类型,并且:

对于 UPDATE,INSERT和DELETE 语句,返回值是受SQL语句影响的行数。



对于所有其他类型的语句,返回值为-1。



ExecuteReader: 使用此操作执行任意SQL语句在SQL Server 中,如果要返回结果集,(如果有),作为DataSet数组。

有关DataSet的信息,请参阅 http://go.microsoft上的DataSet类。 .com / fwlink /?LinkId = 119631。 [ ^ ]

(例如,SELECT col1,col2 from sometable)。



ExecuteScalar: 使用此操作执行 SQL Server 中的任意SQL语句返回单值。

此操作仅返回SQL返回的结果集中第一行的第一列中的值声明。

示例可能是 SELECT @@ IDENTITY AS'Identity'。



参考类似主题:

Execute Scalar,Execute Reader和ExecuteNonQuery之间有什么区别 [ ^ ]

ExecuteNonQuery,ExecuteReader和ExecuteScalar之间有什么区别? [ ^ ]



MSDN:

http://msdn.microsoft.com/ en-us / library / dd787963(v = bts.10).aspx [ ^ ]

http://msdn.microsoft.com/en-us/library/dd788418(v = bts.10).aspx [ ^ ]
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. The return value of this operation is of Int32 data type, and:
For the UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the SQL statement.

For all other types of statements, the return value is -1.

ExecuteReader: Use this operation to execute any arbitrary SQL statements in SQL Server if you want the result set to be returned, if any, as an array of DataSet.
For information about DataSet, see "DataSet Class" at http://go.microsoft.com/fwlink/?LinkId=119631.[^]
(e.g., SELECT col1, col2 from sometable).

ExecuteScalar: Use this operation to execute any arbitrary SQL statements in SQL Server to return a single value.
This operation returns the value only in the first column of the first row in the result set returned by the SQL statement.
An example might be SELECT @@IDENTITY AS 'Identity'.

Refer similar thread:
What is the difference between Execute Scalar , Execute Reader and ExecuteNonQuery[^]
What is the difference between ExecuteNonQuery, ExecuteReader, and ExecuteScalar?[^]

MSDN:
http://msdn.microsoft.com/en-us/library/dd787963(v=bts.10).aspx[^]
http://msdn.microsoft.com/en-us/library/dd788418(v=bts.10).aspx[^]


ExecuteNonQuery():仅适用于Action Queries(创建,更改,删除,插入,更新,删除)。



返回受影响的行数通过查询。

返回类型为int

返回值是可选的,可以分配给整数变量。



ExecuteReader():将使用Action和Non-Action Queries(Select)返回Query选择的行集合。



返回类型为DataReader。

返回值是强制性的,应该分配给另一个对象DataReader。



ExecuteScalar():将使用包含聚合函数的非操作查询。



返回查询结果的第一行和第一列值。

返回类型是对象。

返回值是强制的并应分配给所需类型的变量
ExecuteNonQuery(): will work with Action Queries only (Create,Alter,Drop,Insert,Update,Delete).

Returns the count of rows effected by the Query.
Return type is int
Return value is optional and can be assigned to an integer variable.

ExecuteReader(): will work with Action and Non-Action Queries (Select) Returns the collection of rows selected by the Query.

Return type is DataReader.
Return value is compulsory and should be assigned to an another object DataReader.

ExecuteScalar(): will work with Non-Action Queries that contain aggregate functions.

Return the first row and first column value of the query result.
Return type is object.
Return value is compulsory and should be assigned to a variable of required type


这篇关于asp.net中executecalar和executenonquery以及executereader的返回类型是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆