命令和参数Commands and Parameters

建立与数据源的连接后,可以使用 DbCommand 对象来执行命令并从数据源中返回结果。After establishing a connection to a data source, you can execute commands and return results from the data source using a DbCommand object. 您可以使用命令构造函数之一为要使用的 .NET Framework 数据提供程序创建命令。You can create a command using one of the command constructors for the .NET Framework data provider you are working with. 构造函数可以采用可选自变量,如要在数据源中执行的 SQL 语句、DbConnection 对象或 DbTransaction 对象。Constructors can take optional arguments, such as an SQL statement to execute at the data source, a DbConnection object, or a DbTransaction object. 您也可以将这些对象配置为命令的属性。You can also configure those objects as properties of the command. 也可以使用 CreateCommand 对象的 DbConnection 方法创建用于特定连接的命令。You can also create a command for a particular connection using the CreateCommand method of a DbConnection object. 由命令执行的 SQL 语句可以使用 CommandText 属性进行配置。The SQL statement being executed by the command can be configured using the CommandText property.

随 .NET Framework 提供的每个 .NET Framework 数据提供程序都具有一个 Command 对象。Each .NET Framework data provider included with the .NET Framework has a Command object. 适用于 OLE DB 的 .NET Framework 数据提供程序包括一个 OleDbCommand 对象,适用于 SQL Server 的 .NET Framework 数据提供程序包括一个 SqlCommand 对象,适用于 ODBC 的 .NET Framework 数据提供程序包括一个 OdbcCommand 对象,适用于 Oracle 的 .NET Framework 数据提供程序包括一个 OracleCommand 对象。The .NET Framework Data Provider for OLE DB includes an OleDbCommand object, the .NET Framework Data Provider for SQL Server includes a SqlCommand object, the .NET Framework Data Provider for ODBC includes an OdbcCommand object, and the .NET Framework Data Provider for Oracle includes an OracleCommand object.

本节内容In This Section

执行命令Executing a Command
说明 ADO.NET Command 对象以及如何使用该对象对数据源执行查询和命令。Describes the ADO.NET Command object and how to use it to execute queries and commands against a data source.

配置参数和参数数据类型Configuring Parameters and Parameter Data Types
说明如何使用 Command 参数,包括方向、数据类型和参数语法。Describes working with Command parameters, including direction, data types, and parameter syntax.

使用 CommandBuilder 生成命令Generating Commands with CommandBuilders
说明如何使用命令生成器为具有单表 SELECT 命令的 DataAdapter 自动生成 INSERT、UPDATE 和 DELETE 命令。Describes how to use command builders to automatically generate INSERT, UPDATE, and DELETE commands for a DataAdapter that has a single-table SELECT command.

从数据库获取单一值Obtaining a Single Value from a Database
说明如何使用 ExecuteScalar对象的 Command 方法从数据库查询中返回单个值。Describes how to use the ExecuteScalar method of a Command object to return a single value from a database query.

使用命令修改数据Using Commands to Modify Data
说明如何使用数据提供程序来执行存储过程或数据定义语言 (DDL) 语句。Describes how to use a data provider to execute stored procedures or data definition language (DDL) statements.

请参阅See also