Sideway
output.to from Sideway
Draft for Information Only

Content

System.Data.OleDB
OleDbCommand Class
 Definition
  In this article
 Examples
 Remarks
 Constructors
 Properties
 Methods
 Explicit Interface Implementations
 Events
 Applies to
   .NET Core
   .NET Framework
   .NET Platform Extensions
   Xamarin.Mac
 See also
 Examples
 Source/Reference

System.Data.OleDB

The System.Data.OleDb namespace is the.NET Framework Data Provider for OLE DB.

OleDbCommand Class

Definition

Namespace:
System.Data.OleDb
Assemblies:
System.Data.dll, System.Data.OleDb.dll

Represents an SQL statement or stored procedure to execute against a data source.

In this article

  1. Definition
  2. Examples
  3. Remarks
  4. Constructors
  5. Properties
  6. Methods
  7. Explicit Interface Implementations
  8. Events
  9. Applies to
  10. See also
C#
public sealed class OleDbCommand : System.Data.Common.DbCommand, ICloneable, IDisposable
Inheritance
Object MarshalByRefObject Component OleDbCommand
Implements
IDbCommand ICloneable IDisposable

Examples

The following example uses the OleDbCommand, along OleDbDataAdapter and OleDbConnection, to select rows from an Access database. The filled DataSet is then returned. The example is passed an initialized DataSet, a connection string, a query string that is an SQL SELECT statement, and a string that is the name of the source database table.

C#
public void ReadMyData(string connectionString)
{
    string queryString = "SELECT OrderID, CustomerID FROM Orders";
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand(queryString, connection);
        connection.Open();
        OleDbDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            Console.WriteLine(reader.GetInt32(0) + ", " + reader.GetString(1));
        }
        // always call Close when done reading.
        reader.Close();
    }
}

Remarks

When an instance of OleDbCommand is created, the read/write properties are set to their initial values. For a list of these values, see the OleDbCommand constructor.

OleDbCommand features the following methods executing commands at a data source:

Item Description
ExecuteReader Executes commands that return rows. ExecuteReader may not have the effect that you want if used to execute commands such as SQL SET statements.
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 OleDbCommand object. However, you must close the OleDbDataReader before you can execute a new or previous command.

If a fatal OleDbException (for example, a SQL Server severity level of 20 or greater) is generated by the method executing an OleDbCommand, the OleDbConnection, the connection may be closed. However, the user can reopen the connection and continue.

Constructors

OleDbCommand()

Initializes a new instance of the OleDbCommand class.

OleDbCommand(String)

Initializes a new instance of the OleDbCommand class with the text of the query.

OleDbCommand(String, OleDbConnection)

Initializes a new instance of the OleDbCommand class with the text of the query and an OleDbConnection.

OleDbCommand(String, OleDbConnection, OleDbTransaction)

Initializes a new instance of the OleDbCommand class with the text of the query, an OleDbConnection, and the Transaction.

Properties

CanRaiseEvents

Gets a value indicating whether the component can raise an event.

(Inherited from Component)
CommandText

Gets or sets the SQL statement or stored procedure to execute at the data source.

CommandTimeout

Gets or sets the wait time before terminating an attempt to execute a command and generating an error.

CommandType

Gets or sets a value that indicates how the CommandText property is interpreted.

Connection

Gets or sets the OleDbConnection used by this instance of the OleDbCommand.

Container

Gets the IContainer that contains the Component.

(Inherited from Component)
DesignMode

Gets a value that indicates whether the Component is currently in design mode.

(Inherited from Component)
DesignTimeVisible

Gets or sets a value that indicates whether the command object should be visible in a customized Windows Forms Designer control.

Events

Gets the list of event handlers that are attached to this Component.

(Inherited from Component)
Parameters

Gets the OleDbParameterCollection.

Site

Gets or sets the ISite of the Component.

(Inherited from Component)
Transaction

Gets or sets the OleDbTransaction within which the OleDbCommand executes.

UpdatedRowSource

Gets or sets how command results are applied to the DataRow when used by the Update method of the OleDbDataAdapter.

Methods

Cancel()

Tries to cancel the execution of an OleDbCommand.

Clone()

Creates a new OleDbCommand object that is a copy of the current instance.

CreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
CreateParameter()

Creates a new instance of an OleDbParameter object.

Dispose()

Releases all resources used by the Component.

(Inherited from Component)
Dispose(Boolean)

Releases the unmanaged resources used by the Component and optionally releases the managed resources.

(Inherited from Component)
Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
ExecuteNonQuery()

Executes an SQL statement against the Connection and returns the number of rows affected.

ExecuteReader()

Sends the CommandText to the Connection and builds an OleDbDataReader.

ExecuteReader(CommandBehavior)

Sends the CommandText to the Connection, and builds an OleDbDataReader using one of the CommandBehavior values.

ExecuteScalar()

Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.

GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetService(Type)

Returns an object that represents a service provided by the Component or by its Container.

(Inherited from Component)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
Prepare()

Creates a prepared (or compiled) version of the command on the data source.

ResetCommandTimeout()

Resets the CommandTimeout property to the default value.

ToString()

Returns a String containing the name of the Component, if any. This method should not be overridden.

(Inherited from Component)

Explicit Interface Implementations

ICloneable.Clone()

For a description of this member, see Clone().

IDbCommand.ExecuteReader()

For a description of this member, see ExecuteReader().

IDbCommand.ExecuteReader(CommandBehavior)

Executes the CommandText against the Connection, and builds an IDataReader using one of the CommandBehavior values.

Events

Disposed

Occurs when the component is disposed by a call to the Dispose() method.

(Inherited from Component)

Applies to

.NET Core

3.0 Preview 7

.NET Framework

4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0 1.1

.NET Platform Extensions

3.0 Preview 7

Xamarin.Mac

3.0

See also

Examples

Examples of OleDbCommand Class
ASP.NET Code Input:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
       <title>Sample Page</title>
       <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
       <script runat="server" >
           Sub Page_Load()
               Dim xstr As String
               
               Dim xconn As System.Data.OleDb.OleDbConnection = new System.Data.OleDb.OleDbConnection()
               Dim xcomm As System.Data.OleDb.OleDbCommand = new System.Data.OleDb.OleDbCommand()
               xconn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=T:\test.mdb;User Id=admin;Password=;"
               xconn.Open()
               xstr = xstr + "Database test.mdb connection xconn is opened successfully.<br />"
               xcomm.Connection = xconn
               xstr = xstr + "Command Connection: " & xcomm.Connection.ToString & "<br />"
               xcomm.CommandText = "Select * from t1"
               xstr = xstr + "Command Text: " & xcomm.CommandText & "<br />"
               Dim xreader As System.Data.OleDb.OleDbDataReader = xcomm.ExecuteReader()
               xstr = xstr + "DataReader xreader is assigned successfully.<br />"
               xstr = xstr & "Fieldcount of table t1 is " & xreader.FieldCount & "<br />"
               xconn.Close()
               xstr = xstr + "Database test.mdb connection xconn is closed successfully.<br />"
               xconn.Dispose()
               xstr = xstr + "Database test.mdb connection xconn is disposed successfully."
               lbl01.Text = xstr
           End Sub
       </script>
    </head>
    <body>
<%Response.Write("<p>Results on "& Request.ServerVariables("SERVER_SOFTWARE") & " .net: " & System.Environment.Version.ToString & " " & ScriptEngine & " Version " & ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion & "</p>")%>
       <% Response.Write ("<h1>This is a Sample Page of OleDbCommand Class</h1>") %>
       <p>
           <%-- Set on Page_Load --%>
           <asp:Label id="lbl01" runat="server" />
       </p>
    </body>
</html>
HTML Web Page Embedded Output:

Source/Reference


©sideway

ID: 201000028 Last Updated: 10/28/2020 Revision: 0 Ref:

close

References

  1. Active Server Pages,  , http://msdn.microsoft.com/en-us/library/aa286483.aspx
  2. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
  3. ASP Best Practices,  , http://technet.microsoft.com/en-us/library/cc939157.aspx
  4. ASP Built-in Objects,  , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
  5. Response Object,  , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
  6. Request Object,  , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
  7. Server Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
  8. Application Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
  9. Session Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
  10. ASPError Object,  , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
  11. ObjectContext Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
  12. Debugging Global.asa Files,  , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
  13. How to: Debug Global.asa files,  , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
  14. Calling COM Components from ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
  15. IIS ASP Scripting Reference,  , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
  16. ASP Keywords,  , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
  17. Creating Simple ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
  18. Including Files in ASP Applications,  , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
  19. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
  20. FileSystemObject Object,  , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx,  , ADO Object Model
  22. ADO Fundamentals,  , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx
close

Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 339

Reference 79

Computer

Hardware 249

Software

Application 213

Digitization 32

Latex 52

Manim 205

KB 1

Numeric 19

Programming

Web 289

Unicode 504

HTML 66

CSS 65

SVG 46

ASP.NET 270

OS 429

DeskTop 7

Python 72

Knowledge

Mathematics

Formulas 8

Algebra 84

Number Theory 206

Trigonometry 31

Geometry 34

Coordinate Geometry 2

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1


Copyright © 2000-2024 Sideway . All rights reserved Disclaimers last modified on 06 September 2019