System.Data Namespace
Represents an SQL statement that is executed while connected to a data source, and is implemented by .NET data providers that access relational databases.
Class |
Description |
OleDbCommand |
Represents an SQL statement or stored procedure to execute at a data source. |
SqlCommand |
Represents a Transact-SQL statement or stored procedure to execute at an SQL Server™ database. This class cannot be inherited. |
The IDbCommand interface allows an inheriting class to implement a Command class, which represents an SQL statement that is executed at a data source. For more information about Command classes, see ADO.NET Commands. For more information about implementing .NET data providers, see Implementing a .NET Data Provider.
An application does not create an instance of the IDbCommand interface directly, but initializes an instance of a class that inherits IDbCommand.
Classes that inherit IDbCommand must implement the inherited members, and typically define additional members to add provider-specific functionality. For example, the IDbCommand interface defines the ExecuteNonQuery method. In turn, the SqlCommand class inherits this method, and also defines the ExecuteXmlReader method.
Notes to Implementers: To promote consistency among .NET data providers, name the inheriting class in the form Prv Command where Prv is the uniform prefix given to all classes in a specific .NET data provider namespace. For example, Sql is the prefix of the SqlCommand class in the System.Data.SqlClient namespace.
When you inherit from the IDbCommand interface, you should implement the following constructors:
Item |
Description |
PrvCommand ( ) |
Initializes a new instance of the PrvCommand class. |
PrvCommand ( string cmdText ) |
Initializes a new instance of the PrvCommand class with the text of the query. |
PrvCommand ( string cmdText, PrvConnection connection ) |
Initializes a new instance of the PrvCommand class with the text of the query and a PrvConnection. |
PrvCommand ( string cmdText, PrvConnection connection, PrvTransaction transaction ) |
Initializes a new instance of the PrvCommand class with the text of the query, a PrvConnection, and the PrvTransaction. |
The following example initializes instances of the derived classes, SqlConnection, SqlCommand, and SqlDataReader. The example reads through the data, writing it to the page. Finally, the example closes the SqlDataReader, then the SqlConnection.
void ReadMyData ( string connString ) {
string query = "SELECT OrderID, Customer FROM Orders";
SqlConnection myConn = new SqlConnection ( connString );
SqlCommand myCmd = new SqlCommand ( query, myConn );
myConn.Open ( );
SqlDataReader myReader = myCmd.ExecuteReader ( );
try {
while ( myReader.Read ( ) ) {
Response.Write ( myReader.GetInt32 ( 0 ) +
", " + myReader.GetString ( 1 ) );
}
}
finally {
// always close the reader when done.
myReader.Close ( );
// always close the connection when done.
myConn.Close ( );
}
}
Public Sub ReadMyData ( connString As String )
Dim query As String = "SELECT OrderID, Customer FROM Orders"
Dim myConn As New SqlConnection ( connString )
Dim myCmd As New SqlCommand ( query, myConn )
myConn.Open ( )
Dim myReader As SqlDataReader = myCmd.ExecuteReader ( )
Try
While myReader.Read ( )
Response.Write ( ( myReader.GetInt32 ( 0 ).ToString & _
", " & myReader.GetString ( 1 ) ) )
End While
Finally
' always close the reader when done.
myReader.Close ( )
' always close the connection when done.
myConn.Close ( )
End Try
End Sub |
|
C# |
VB |
DbDataAdapter IDbConnection OleDbCommand SqlCommand