ADO.NET常用对象详解之:Command对象

  1.Command对象概述
  Command对象可以使用数据库命令直接与数据源进行通信。它的属性如下:
  Name:Command对象的程序化名称。在代码中使用此名称来引用Command对象。
  Connection:对Connection对象的引用,Command对象将使用该对象与数据库通信。
  CommandType:Text | StoreProduce | TableDirect。
  CommandText:SQL语句 | StoreProduce。
  Parameters:命令对象包含的参数。
 
  可以通过一下三种方式来创建Command对象
方式一
方式二
方式三

  2.Command对象参数
  常用属性如下:
  ParameterName:参数名称,如"@CatID"。
  DbType,SqlType,OleDbType:参数的数据类型。
  Direction:ParameterDirection枚举值。
                   ParameterDirection.Input(默认值)| ParameterDirection.InputOutput |
                   ParameterDirection.Output | ParameterDirection.ReturnValue

  3.执行
  四种执行方式:
ExecuteNonQuery() 返回受命令影响的行数。
ExecuteScalar()   返回第一行第一列(使用与集函数)。如Count(*),Sum,Avg等聚合函数。       
ExecuteReader() 返回一个DataReader对象。如果SQL不是查询Select,则返回一个没有任何数据的System.Data.SqlClient.SqlDataReader类型的集合(EOF)。
ExecuteXmlReader()返回一个XmlReader对象。

  4.参数化查询
使用示例
注:SQL Server .Net数据提供程序不支持通用的参数标记"?",而需要用"@"做前缀来命名参数。

posted on 2006-09-04 14:18  头发乱了  阅读(1297)  评论(0编辑  收藏  举报