Represents an SQL statement or stored procedure to execute against a data source. This class cannot be inherited.
Namespace:
System.Data.Odbc
Assembly:
System.Data (in System.Data.dll)
Visual Basic (Declaration)
Public NotInheritable Class OdbcCommand _
Inherits DbCommand _
Implements ICloneable
Dim instance As OdbcCommand
public sealed class OdbcCommand : DbCommand,
ICloneable
public ref class OdbcCommand sealed : public DbCommand,
ICloneable
public final class OdbcCommand extends DbCommand implements ICloneable
The OdbcCommand class provides the following methods for executing commands against a data source:
Item | Description |
|---|
ExecuteReader | Executes commands that return rows. |
ExecuteNonQuery | Executes commands such as SQL INSERT, DELETE, UPDATE, and SET statements. |
ExecuteScalar | Retrieves a single value, for example, an aggregate value, from a database. |
You can reset the CommandText property and reuse the OdbcCommand object. However, you must close the OdbcDataReader before you can execute a new or previous command.
If execution of the command causes a fatal OdbcException such as a SQL Server severity level of 20 or more, OdbcConnection may close. However, the user can reopen the connection and continue.
The following example uses the ExecuteReader method of the OdbcCommand class, together with the OdbcDataReader and OdbcConnection classes, to select rows from a table.
Public Sub InsertRow(ByVal connectionString As String, _
ByVal insertSQL As String)
Using connection As New OdbcConnection(connectionString)
' The insertSQL string contains a SQL statement that
' inserts a new row in the source table.
Dim command As New OdbcCommand(insertSQL, connection)
' Open the connection and execute the insert command.
Try
connection.Open()
command.ExecuteNonQuery()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
' The connection is automatically closed when the
' code exits the Using block.
End Using
End Sub
public void InsertRow(string connectionString, string insertSQL)
{
using (OdbcConnection connection =
new OdbcConnection(connectionString))
{
// The insertSQL string contains a SQL statement that
// inserts a new row in the source table.
OdbcCommand command = new OdbcCommand(insertSQL, connection);
// Open the connection and execute the insert command.
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// The connection is automatically closed when the
// code exits the using block.
}
System..::.Object
System..::.MarshalByRefObject
System.ComponentModel..::.Component
System.Data.Common..::.DbCommand
System.Data.Odbc..::.OdbcCommand
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1
Reference
Other Resources