何时使用ExecuteScalar,ExecuteReader和ExecuteNonQuery? [英] When to use ExecuteScalar, ExecuteReader, and ExecuteNonQuery?
本文介绍了何时使用ExecuteScalar,ExecuteReader和ExecuteNonQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对
I am confused with the usage of
- ExecuteScalar
- ExecuteReader $的用法感到困惑在我的代码中执行SQL查询时,b $ b
- ExecuteNonQuery
- ExecuteScalar
- ExecuteReader
- ExecuteNonQuery
什么时候应该使用这些方法?
when executing SQL queries in my code. When should I use each of these methods?
推荐答案
-
ExecuteScalar()
仅返回查询第一行第一列中的值。 -
ExecuteReader()
返回一个对象,该对象可以遍历整个结果集,而一次只在内存中保留一条记录。 -
ExecuteNonQuery()
完全不返回数据:仅受插入,更新或删除影响的行数。 ExecuteScalar()
only returns the value from the first column of the first row of your query.ExecuteReader()
returns an object that can iterate over the entire result set while only keeping one record in memory at a time.ExecuteNonQuery()
does not return data at all: only the number of rows affected by an insert, update, or delete.
此外,您还可以查看 DbDataAdapter
类型,其中包括 Fill()
方法,使您可以将整个结果集下载到 DataTable
或 DataSet
对象,以及许多其他功能。
Additionally, you can look at the DbDataAdapter
type, which includes a Fill()
method that allows you to download the entire resultset into a DataTable
or DataSet
object, as well as a number of other abilities.
最后,这似乎是您获取的好时机熟悉 Microsoft文档。这就是为之准备的文档:您拥有方法名称;去查一下。
Finally, this seems like a good time for you to get familiar with Microsoft Docs. This is what documentation is made for: you have the method names; go look them up.
这篇关于何时使用ExecuteScalar,ExecuteReader和ExecuteNonQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文