ADO.NET入门教程之ExecuteNonQuery, ExecuteScalar, and ExecuteReader

Now let’s take a look at the three Execute methods of the SqlCommand class: ExecuteNonQuery, ExecuteScalar, and ExecuteReader.
ExecuteNonQuery is used to execute a SQL statement or stored procedure that doesn’t
return any records. You’ll use this method when executing operations that update, insert, or
delete information in the database. ExecuteNonQuery returns an integer value that specifies how
many rows were affected by the query—this proves useful if you want to know, for example,
how many rows were deleted by the last delete operation. Of course, in case you don’t need to
know that number, you can simply ignore the return value. Here’s a simple piece of code that
shows how to open the connection, execute the command using ExecuteNonQuery, and imme-
diately close the connection afterward:
    connection.Open();
    command.ExecuteNonQuery();
    command.Close();
ExecuteScalar is like ExecuteNonQuery in that it returns a single value, although it returns a
value that has been read from the database instead of the number of affected rows. It is used in
conjunction with SELECT statements that select a single value. If SELECT returns more rows and/or
more columns, only the first column in the first row is returned. A typical SQL query that should be
executed using ExecuteScalar is SELECT COUNT(*) FROM Department—which returns the
number of rows in the Department table.
ExecuteReader is used with SELECT statements that return multiple records (with any
number of fields). ExecuteReader returns a SqlDataReader object, which contains the results of
the query. A SqlDataReader object reads and returns the results one by one, in a forward-only
and read-only manner. The good news about the SqlDataReader is that it represents the fastest
way to read data from the database, and the bad news is that it needs an open connection to
operate—no other database operations can be performed on that connection until the reader is closed. In our solution, you’ll load all the data returned by the SqlDataReader into a DataTable
object (which is capable of storing the data offline without needing an open connection),
which will allow you to close the database connection very quickly.

The DataTable class can store a result set locally without needing an open connection to
SQL Server, and it isn’t data provider–specific, like the other ADO.NET objects mentioned so
far (whose names begin with Sql because they’re SQL Server–specific).

 A “parent” of the DataTable object is the DataSet, which is a very smart object that represents
something like an in-memory database. DataSet is capable of storing data tables, their data types, relation-
ships between tables, and so on. Because of their complexity, DataSets consume a lot of memory, so it’s
good to avoid them when possible.

 

Not original,copied from, :Beginning ASP.NET E-Commerce in C# From Novice to Professional

 

转载于:https://www.cnblogs.com/elock/archive/2009/12/04/1616811.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值