Permalink
Fetching contributors…
Cannot retrieve contributors at this time
50 lines (42 sloc) 3.62 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic ms.assetid caps.latest.revision author ms.author manager
Executing a Command
03/30/2017
.net-framework
dotnet-ado
article
40494916-c25a-4cb8-8f7c-fcb8d378464e
3
JennieHubbard
jhubbard
jhubbard

Executing a Command

Each .NET Framework data provider included with the .NET Framework has its own command object that inherits from xref:System.Data.Common.DbCommand. The .NET Framework Data Provider for OLE DB includes an xref:System.Data.OleDb.OleDbCommand object, the .NET Framework Data Provider for SQL Server includes a xref:System.Data.SqlClient.SqlCommand object, the .NET Framework Data Provider for ODBC includes an xref:System.Data.Odbc.OdbcCommand object, and the .NET Framework Data Provider for Oracle includes an xref:System.Data.OracleClient.OracleCommand object. Each of these objects exposes methods for executing commands based on the type of command and desired return value, as described in the following table.

Command Return Value
ExecuteReader Returns a DataReader object.
ExecuteScalar Returns a single scalar value.
ExecuteNonQuery Executes a command that does not return any rows.
ExecuteXMLReader Returns an xref:System.Xml.XmlReader. Available for a SqlCommand object only.

Each strongly typed command object also supports a xref:System.Data.CommandType enumeration that specifies how a command string is interpreted, as described in the following table.

CommandType Description
Text An SQL command defining the statements to be executed at the data source.
StoredProcedure The name of the stored procedure. You can use the Parameters property of a command to access input and output parameters and return values, regardless of which Execute method is called. When using ExecuteReader, return values and output parameters will not be accessible until the DataReader is closed.
TableDirect The name of a table.

Example

The following code example demonstrates how to create a xref:System.Data.SqlClient.SqlCommand object to execute a stored procedure by setting its properties. A xref:System.Data.SqlClient.SqlParameter object is used to specify the input parameter to the stored procedure. The command is executed using the xref:System.Data.SqlClient.SqlCommand.ExecuteReader%2A method, and the output from the xref:System.Data.SqlClient.SqlDataReader is displayed in the console window.

[!code-csharp[DataWorks SqlClient.StoredProcedure#1](../../../../samples/snippets/csharp/VS_Snippets_ADO.NET/DataWorks SqlClient.StoredProcedure/CS/source.cs#1)] [!code-vb[DataWorks SqlClient.StoredProcedure#1](../../../../samples/snippets/visualbasic/VS_Snippets_ADO.NET/DataWorks SqlClient.StoredProcedure/VB/source.vb#1)]

Troubleshooting Commands

The .NET Framework Data Provider for SQL Server adds performance counters to enable you to detect intermittent problems related to failed command executions. For more information see Performance Counters.

See Also

Commands and Parameters
DataAdapters and DataReaders
Working with DataReaders
ADO.NET Managed Providers and DataSet Developer Center