Draft for Information Only
Content
System.Data.OleDB OleDbDataAdapter Class Definition In this article Examples Remarks Constructors Properties Methods Events Explicit Interface Implementations Fields 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.
OleDbDataAdapter Class
Definition
Namespace:
System.Data.OleDb
Assemblies:
System.Data.dll, System.Data.OleDb.dll
Represents a set of data commands and a database connection that are used to fill the DataSet and update the data source.
In this article
Definition
Examples
Remarks
Constructors
Properties
Methods
Events
Explicit Interface Implementations
Fields
Applies to
See also
C#
public sealed class OleDbDataAdapter : System.Data.Common.DbDataAdapter, ICloneable
Inheritance
Object
MarshalByRefObject
Component
DataAdapter
DbDataAdapter
OleDbDataAdapter
Implements
IDataAdapter IDbDataAdapter ICloneable
Examples
The following example uses the OleDbCommand , OleDbDataAdapter and OleDbConnection , to select records from an Access data source, and populate a DataSet with the selected rows. The filled DataSet is then returned. To accomplish this, the method
is passed an initialized DataSet , a connection string, and a query string that is an SQL SELECT statement.
C#
public static OleDbDataAdapter CreateDataAdapter(string selectCommand,
OleDbConnection connection)
{
OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand, connection);
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
// Create the Insert, Update and Delete commands.
adapter.InsertCommand = new OleDbCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (?, ?)");
adapter.UpdateCommand = new OleDbCommand(
"UPDATE Customers SET CustomerID = ?, CompanyName = ? " +
"WHERE CustomerID = ?");
adapter.DeleteCommand = new OleDbCommand(
"DELETE FROM Customers WHERE CustomerID = ?");
// Create the parameters.
adapter.InsertCommand.Parameters.Add("@CustomerID",
OleDbType.Char, 5, "CustomerID");
adapter.InsertCommand.Parameters.Add("@CompanyName",
OleDbType.VarChar, 40, "CompanyName");
adapter.UpdateCommand.Parameters.Add("@CustomerID",
OleDbType.Char, 5, "CustomerID");
adapter.UpdateCommand.Parameters.Add("@CompanyName",
OleDbType.VarChar, 40, "CompanyName");
adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
OleDbType.Char, 5, "CustomerID").SourceVersion =
DataRowVersion.Original;
adapter.DeleteCommand.Parameters.Add("@CustomerID",
OleDbType.Char, 5, "CustomerID").SourceVersion =
DataRowVersion.Original;
return adapter;
}
Remarks
The OleDbDataAdapter serves as a bridge between a DataSet and data source for retrieving and saving data. The OleDbDataAdapter provides this bridge by using Fill to load data from the data source into the DataSet , and using
Update to send changes made in the DataSet back to the data source.
When the OleDbDataAdapter fills a DataSet , it will create the appropriate tables and columns for the returned data if they do not already exist. However, primary key information is not included in the implicitly created schema unless the MissingSchemaAction property is set to AddWithKey . You may also have the
OleDbDataAdapter create the schema of the DataSet , including primary key information, before filling it with data using FillSchema . For more information, see Adding Existing Constraints to a DataSet .
Note that some OLE DB providers, including the MSDataShape provider, do not return base table or primary key information. Therefore, the OleDbDataAdapter cannot correctly set the PrimaryKey property on any created DataTable . In these cases you should explicitly specify primary keys for tables in the DataSet .
The OleDbDataAdapter also includes the SelectCommand , InsertCommand , DeleteCommand , UpdateCommand , and
TableMappings properties to facilitate the loading and updating of data.
When you create an instance of OleDbDataAdapter , properties are set to their initial values. For a list of these values, see the OleDbDataAdapter constructor.
Constructors
Properties
Methods
AddToBatch(IDbCommand)
Adds a IDbCommand to the current batch.
(Inherited from DbDataAdapter )
ClearBatch()
Removes all IDbCommand objects from the batch.
(Inherited from DbDataAdapter )
CloneInternals()
Creates a copy of this instance of DataAdapter .
(Inherited from DataAdapter )
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 )
CreateRowUpdatedEvent(DataRow, IDbCommand, StatementType, DataTableMapping)
Initializes a new instance of the RowUpdatedEventArgs class.
(Inherited from DbDataAdapter )
CreateRowUpdatingEvent(DataRow, IDbCommand, StatementType, DataTableMapping)
Initializes a new instance of the RowUpdatingEventArgs class.
(Inherited from DbDataAdapter )
CreateTableMappings()
Creates a new DataTableMappingCollection .
(Inherited from DataAdapter )
Dispose()
Releases all resources used by the Component .
(Inherited from Component )
Dispose(Boolean)
Releases the unmanaged resources used by the DbDataAdapter and optionally releases the managed resources.
(Inherited from DbDataAdapter )
Equals(Object)
Determines whether the specified object is equal to the current object.
(Inherited from Object )
ExecuteBatch()
Executes the current batch.
(Inherited from DbDataAdapter )
Fill(DataSet)
Adds or refreshes rows in the DataSet .
(Inherited from DbDataAdapter )
Fill(DataSet, Int32, Int32, String)
Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet and DataTable names.
(Inherited from DbDataAdapter )
Fill(DataSet, Int32, Int32, String, IDbCommand, CommandBehavior)
Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet and source table names, command string, and command behavior.
(Inherited from DbDataAdapter )
Fill(DataSet, Object, String)
Adds or refreshes rows in the DataSet to match those in an ADO Recordset or Record object using the specified DataSet , ADO object, and source table name.
Fill(DataSet, String)
Adds or refreshes rows in the DataSet to match those in the data source using the DataSet and DataTable names.
(Inherited from DbDataAdapter )
Fill(DataTable)
Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataTable name.
(Inherited from DbDataAdapter )
Fill(DataTable, IDbCommand, CommandBehavior)
Adds or refreshes rows in a DataTable to match those in the data source using the specified DataTable , IDbCommand and CommandBehavior .
(Inherited from DbDataAdapter )
Fill(DataTable, Object)
Adds or refreshes rows in a DataTable to match those in an ADO Recordset or Record object using the specified DataTable and ADO objects.
Fill(DataTable[], IDataReader, Int32, Int32)
Adds or refreshes rows in a specified range in the collection of DataTable objects to match those in the data source.
(Inherited from DataAdapter )
Fill(DataTable[], Int32, Int32, IDbCommand, CommandBehavior)
Adds or refreshes rows in a specified range in the DataSet to match those in the data source using the DataSet and DataTable names.
(Inherited from DbDataAdapter )
Fill(Int32, Int32, DataTable[])
Adds or refreshes rows in a DataTable to match those in the data source starting at the specified record and retrieving up to the specified maximum number of records.
(Inherited from DbDataAdapter )
FillSchema(DataSet, SchemaType)
Adds a DataTable named "Table" to the specified DataSet and configures the schema to match that in the data source based on the specified SchemaType .
(Inherited from DbDataAdapter )
FillSchema(DataSet, SchemaType, IDbCommand, String, CommandBehavior)
Adds a DataTable to the specified DataSet and configures the schema to match that in the data source based on the specified SchemaType .
(Inherited from DbDataAdapter )
FillSchema(DataSet, SchemaType, String)
Adds a DataTable to the specified DataSet and configures the schema to match that in the data source based upon the specified SchemaType and DataTable .
(Inherited from DbDataAdapter )
FillSchema(DataSet, SchemaType, String, IDataReader)
Adds a DataTable to the specified DataSet .
(Inherited from DataAdapter )
FillSchema(DataTable, SchemaType)
Configures the schema of the specified DataTable based on the specified SchemaType .
(Inherited from DbDataAdapter )
FillSchema(DataTable, SchemaType, IDataReader)
Adds a DataTable to the specified DataSet .
(Inherited from DataAdapter )
FillSchema(DataTable, SchemaType, IDbCommand, CommandBehavior)
Configures the schema of the specified DataTable based on the specified SchemaType , command string, and CommandBehavior values.
(Inherited from DbDataAdapter )
GetBatchedParameter(Int32, Int32)
Returns a IDataParameter from one of the commands in the current batch.
(Inherited from DbDataAdapter )
GetBatchedRecordsAffected(Int32, Int32, Exception)
Returns information about an individual update attempt within a larger batched update.
(Inherited from DbDataAdapter )
GetFillParameters()
Gets the parameters set by the user when executing an SQL SELECT statement.
(Inherited from DbDataAdapter )
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 )
HasTableMappings()
Indicates whether a DataTableMappingCollection has been created.
(Inherited from DataAdapter )
InitializeBatching()
Initializes batching for the DbDataAdapter .
(Inherited from DbDataAdapter )
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 )
OnRowUpdated(RowUpdatedEventArgs)
Raises the RowUpdated event of a .NET Framework data provider.
(Inherited from DbDataAdapter )
OnRowUpdating(RowUpdatingEventArgs)
Raises the RowUpdating event of a .NET Framework data provider.
(Inherited from DbDataAdapter )
ResetFillLoadOption()
Resets FillLoadOption to its default state and causes Fill(DataSet) to honor AcceptChangesDuringFill .
(Inherited from DataAdapter )
ShouldSerializeAcceptChangesDuringFill()
Determines whether the AcceptChangesDuringFill property should be persisted.
(Inherited from DataAdapter )
ShouldSerializeFillLoadOption()
Determines whether the FillLoadOption property should be persisted.
(Inherited from DataAdapter )
ShouldSerializeTableMappings()
Determines whether one or more DataTableMapping objects exist and they should be persisted.
(Inherited from DataAdapter )
TerminateBatching()
Ends batching for the DbDataAdapter .
(Inherited from DbDataAdapter )
ToString()
Returns a String containing the name of the Component , if any. This method should not be overridden.
(Inherited from Component )
Update(DataRow[])
Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified array in the DataSet .
(Inherited from DbDataAdapter )
Update(DataRow[], DataTableMapping)
Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified array of DataSet objects.
(Inherited from DbDataAdapter )
Update(DataSet)
Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified DataSet .
(Inherited from DbDataAdapter )
Update(DataSet, String)
Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the DataSet with the specified DataTable name.
(Inherited from DbDataAdapter )
Update(DataTable)
Updates the values in the database by executing the respective INSERT, UPDATE, or DELETE statements for each inserted, updated, or deleted row in the specified DataTable .
(Inherited from DbDataAdapter )
Events
Disposed
Occurs when the component is disposed by a call to the Dispose() method.
(Inherited from Component )
RowUpdated
Occurs during Update(DataSet) after a command is executed against the data source. The attempt to update is made. Therefore, the event occurs.
RowUpdating
Occurs during Update(DataSet) before a command is executed against the data source. The attempt to update is made. Therefore, the event occurs.
Explicit Interface Implementations
Fields
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 OleDbDataAdapter 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 New System.Data.OleDb.OleDbConnection
xconn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=T:\test.mdb;User Id=admin;Password=;"
xconn.Open()
xstr = xstr + "Connection xconn to database test.mdb is opened successfully.<br />"
Dim xdata, ydata, zdata As New System.Data.DataSet
Dim xadapt, yadapt As System.Data.OleDb.OleDbDataAdapter
Dim sql As String
sql = "SELECT * FROM T1"
xadapt = New System.Data.OleDb.OleDbDataAdapter()
Dim xcomm As System.Data.OleDb.OleDbCommand = new System.Data.OleDb.OleDbCommand()
xcomm.Connection = xconn
xcomm.CommandText = "Select * from T1"
xadapt.SelectCommand = xcomm
xstr = xstr + "Dataadapter xadapt is assigned to SELECT * FROM T1 through xconn successfully.<br />"
xadapt.Fill(xdata,"T1")
xstr = xstr + "Dataset xdata is filled with dataadapter xadapt.fill successfully.<br />"
xstr = xstr + "Dataset xdata.tables(T1).rows(0).item(1):" + xdata.Tables("T1").Rows(0).Item("f1") + "<br />"
Dim x1 As String ="5"
Dim x2 As String ="6"
xcomm.CommandText = "INSERT INTO T1 (f1, f2) VALUES ('"+x1+"', '"+x2+"')"
xadapt.InsertCommand = xcomm
Dim row As System.Data.DataRow = xdata.Tables(0).NewRow()
row("f1") = x1
row("f2") = x2
xdata.Tables(0).Rows.Add(row)
xstr = xstr + "One row is inserted to Dataset xdata successfully.<br />"
Dim addedRows As System.Data.DataRow() = xdata.Tables("T1").Select(Nothing, Nothing, System.Data.DataViewRowState.Added)
xadapt.Update(addedRows)
xstr = xstr + "Database T1 is updated with dataadapter xadapt.update(addedrows) successfully.<br />"
x1 ="1"
x2 ="7"
xcomm.CommandText = "UPDATE T1 SET f2 = '"+x2+"' WHERE f1= '"+x1+"'"
xadapt.UpdateCommand = xcomm
row = xdata.Tables(0).Rows(0)
row("f2") = x2
xstr = xstr + "One row of Dataset xdata is updated successfully.<br />"
Dim modifiedRows As System.Data.DataRow() = xdata.Tables("T1").Select(Nothing, Nothing, System.Data.DataViewRowState.ModifiedCurrent)
xadapt.Update(modifiedRows)
xstr = xstr + "Database T1 is updated with dataadapter xadapt.update(modifiedrows) successfully.<br />"
x1 ="5"
xcomm.CommandText = "DELETE FROM T1 WHERE f1= '"+x1+"'"
xadapt.DeleteCommand = xcomm
row = xdata.Tables(0).Rows(2)
row.Delete()
xstr = xstr + "One row is deleted from Dataset xdata successfully.<br />"
Dim deletedRows As System.Data.DataRow() = xdata.Tables("T1").Select(Nothing, Nothing, System.Data.DataViewRowState.Deleted)
xadapt.Update(deletedRows)
xstr = xstr + "Database T1 is updated with dataadapter xadapt.update(deletedrows) successfully.<br />"
xadapt.Dispose()
xdata.Dispose()
xconn.Close()
xstr = xstr + "Connection xconn is closed successfully.<br />"
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 OleDbDataAdapter 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: 201000030 Last Updated: 10/30/2020 Revision: 0 Ref:
References
Active Server Pages, , http://msdn.microsoft.com/en-us/library/aa286483.aspx
ASP Overview, , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
ASP Best Practices, , http://technet.microsoft.com/en-us/library/cc939157.aspx
ASP Built-in Objects, , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
Response Object, , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
Request Object, , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
Server Object (IIS), , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
Application Object (IIS), , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
Session Object (IIS), , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
ASPError Object, , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
ObjectContext Object (IIS), , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
Debugging Global.asa Files, , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
How to: Debug Global.asa files, , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
Calling COM Components from ASP Pages, , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
IIS ASP Scripting Reference, , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
ASP Keywords, , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
Creating Simple ASP Pages, , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
Including Files in ASP Applications, , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
ASP Overview, , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
FileSystemObject Object, , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx, , ADO Object Model
ADO Fundamentals, , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx