凡使用执行标量,执行器和数据集? [英] Where to use Execute Scalar,Execute Reader and Data Set?
问题描述
我知道我可以从数据库中使用这三个术语中读取数据,但任何一个可以请指导我正确的时候使用的ExecuteScalar
,的ExecuteReader
和数据集
??
在使用的ExecuteScalar
的ExecuteScalar
:用它来执行任何用户定义的函数或存储过程,通常有一个标量返回值。即只返回一个值。但是,它也可以与其他查询或存储过程中使用,但在这种情况下它返回的仅的第一行中查询所返回的结果集的第一列。其他列或行被忽略。
在使用的ExecuteReader
<一个href="http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executereader%28v=vs.71%29.aspx"相对=nofollow> 的ExecuteReader
:用它为您带来的数据库或存储过程通常的查询。该 CommendText
属性是什么决定它是否是一个查询或存储过程的名称。它发出了的CommandText
来连接并生成一个 SqlDataReader的
返回一组。
数据集是一个不同的事情比的ExecuteReader
和的ExecuteScalar
它重新presents在内存中缓存数据通常来自数据库中。
I know I can read data from database using these three terms but can Any one please guide me properly when to use ExecuteScalar
, ExecuteReader
and DataSet
??
When to use ExecuteScalar
ExecuteScalar
: use it to execute any user defined function or the stored procedures, that usually has a scalar return value. i.e returns only one value. However, it can also be used with other queries or stored procedures, but in this case it returns only the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
When to use ExecuteReader
ExecuteReader
: use it for the usual queries that you pose to the database or with the stored procedure. The CommendText
property is what determines whether it is a query or a stored procedure name. It sends the CommandText
to the Connection and builds a SqlDataReader
that returns a set.
Data set is a different thing than the ExecuteReader
and ExecuteScalar
it represents an in-memory cache of data that are usually coming from the database.
这篇关于凡使用执行标量,执行器和数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!