博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ADO.NET入门教程之ExecuteNonQuery, ExecuteScalar, and ExecuteReader
阅读量:4322 次
发布时间:2019-06-06

本文共 2937 字,大约阅读时间需要 9 分钟。

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

你可能感兴趣的文章
ASP.NET MVC+EF框架+EasyUI实现权限管理(附源码)
查看>>
sitecore系统教程之体验编辑器中创建一个项目
查看>>
socket笔记
查看>>
Java 概述及安装使用
查看>>
helloworld
查看>>
港交所OMD-C对接笔记
查看>>
线程安全问题了解一下
查看>>
转:IPv4的地址真的用光了吗
查看>>
java rmi远程方法调用实例
查看>>
Linux设置环境变量小结
查看>>
syslog()用法
查看>>
Java 内存区域和GC机制
查看>>
STM32上不使用外部晶振,OSC_IN和OSC_OUT的接法
查看>>
设计模式六大原则
查看>>
android中的数据库操作
查看>>
当用updatepanel和scriptmanager时,弹出框
查看>>
如何破解百度云大文件下载限制
查看>>
冒泡排序
查看>>
react中<link>和<navlink>区别
查看>>
C# 生成随机数
查看>>