Click or drag to resize

MySqlCommandBuilder Class

Automatically generates single-table commands used to reconcile changes made to a DataSet with the associated MySQL database. This class cannot be inherited.
Inheritance Hierarchy

Namespace:  MySql.Data.MySqlClient
Assembly:  MySql.Data (in MySql.Data.dll) Version: 8.0.22
Syntax
public sealed class MySqlCommandBuilder : DbCommandBuilder

The MySqlCommandBuilder type exposes the following members.

Constructors
  NameDescription
Public methodMySqlCommandBuilder
Initializes a new instance of the MySqlCommandBuilder class.
Public methodMySqlCommandBuilder(MySqlDataAdapter)
Initializes a new instance of the MySqlCommandBuilder class with the associated MySqlDataAdapter object.
Top
Properties
  NameDescription
Protected propertyCanRaiseEvents
Gets a value indicating whether the component can raise an event.
(Inherited from Component.)
Public propertyCatalogLocation
Sets or gets the CatalogLocation for an instance of the DbCommandBuilder class.
(Inherited from DbCommandBuilder.)
Public propertyCatalogSeparator
Sets or gets a string used as the catalog separator for an instance of the DbCommandBuilder class.
(Inherited from DbCommandBuilder.)
Public propertyConflictOption
Specifies which ConflictOption is to be used by the DbCommandBuilder.
(Inherited from DbCommandBuilder.)
Public propertyContainer
Gets the IContainer that contains the Component.
(Inherited from Component.)
Public propertyDataAdapter
Gets or sets a MySqlDataAdapter object for which SQL statements are automatically generated.
Protected propertyDesignMode
Gets a value that indicates whether the Component is currently in design mode.
(Inherited from Component.)
Protected propertyEvents
Gets the list of event handlers that are attached to this Component.
(Inherited from Component.)
Public propertyQuotePrefix
Gets or sets the beginning character or characters to use when specifying database objects (for example, tables or columns) whose names contain characters such as spaces or reserved tokens.
(Inherited from DbCommandBuilder.)
Public propertyQuoteSuffix
Gets or sets the ending character or characters to use when specifying database objects (for example, tables or columns) whose names contain characters such as spaces or reserved tokens.
(Inherited from DbCommandBuilder.)
Public propertySchemaSeparator
Gets or sets the character to be used for the separator between the schema identifier and any other identifiers.
(Inherited from DbCommandBuilder.)
Public propertySetAllValues
Specifies whether all column values in an update statement are included or only changed ones.
(Inherited from DbCommandBuilder.)
Public propertySite
Gets or sets the ISite of the Component.
(Inherited from Component.)
Top
Methods
  NameDescription
Protected methodApplyParameterInfo (Overrides DbCommandBuilderApplyParameterInfo(DbParameter, DataRow, StatementType, Boolean).)
Public methodCreateObjRef
Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.
(Inherited from MarshalByRefObject.)
Public methodStatic memberDeriveParameters
Retrieves parameter information from the stored procedure specified in the MySqlCommand and populates the Parameters collection of the specified MySqlCommand object. This method is not currently supported since stored procedures are not available in MySql.
Public methodDispose
Releases all resources used by the Component.
(Inherited from Component.)
Protected methodDispose(Boolean)
Releases the unmanaged resources used by the DbCommandBuilder and optionally releases the managed resources.
(Inherited from DbCommandBuilder.)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection.
(Inherited from Component.)
Public methodGetDeleteCommand
Gets the delete command.
Public methodGetDeleteCommand(Boolean)
Gets the automatically generated DbCommand object required to perform deletions at the data source, optionally using columns for parameter names.
(Inherited from DbCommandBuilder.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetInsertCommand
Gets the insert command.
Public methodGetInsertCommand(Boolean)
Gets the automatically generated DbCommand object required to perform insertions at the data source, optionally using columns for parameter names.
(Inherited from DbCommandBuilder.)
Public methodGetLifetimeService
Retrieves the current lifetime service object that controls the lifetime policy for this instance.
(Inherited from MarshalByRefObject.)
Protected methodGetParameterName(Int32) (Overrides DbCommandBuilderGetParameterName(Int32).)
Protected methodGetParameterName(String)
Protected methodGetParameterPlaceholder (Overrides DbCommandBuilderGetParameterPlaceholder(Int32).)
Protected methodGetSchemaTable (Overrides DbCommandBuilderGetSchemaTable(DbCommand).)
Protected methodGetService
Returns an object that represents a service provided by the Component or by its Container.
(Inherited from Component.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodGetUpdateCommand
Gets the update command.
Public methodGetUpdateCommand(Boolean)
Gets the automatically generated DbCommand object required to perform updates at the data source, optionally using columns for parameter names.
(Inherited from DbCommandBuilder.)
Protected methodInitializeCommand
Resets the CommandTimeout, Transaction, CommandType, and UpdateRowSource properties on the DbCommand.
(Inherited from DbCommandBuilder.)
Public methodInitializeLifetimeService
Obtains a lifetime service object to control the lifetime policy for this instance.
(Inherited from MarshalByRefObject.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodMemberwiseClone(Boolean)
Creates a shallow copy of the current MarshalByRefObject object.
(Inherited from MarshalByRefObject.)
Public methodQuoteIdentifier (Overrides DbCommandBuilderQuoteIdentifier(String).)
Public methodRefreshSchema
Clears the commands associated with this DbCommandBuilder.
(Inherited from DbCommandBuilder.)
Protected methodRowUpdatingHandler
Adds an event handler for the RowUpdating event.
(Inherited from DbCommandBuilder.)
Protected methodSetRowUpdatingHandler (Overrides DbCommandBuilderSetRowUpdatingHandler(DbDataAdapter).)
Public methodToString
Returns a String containing the name of the Component, if any. This method should not be overridden.
(Inherited from Component.)
Public methodUnquoteIdentifier (Overrides DbCommandBuilderUnquoteIdentifier(String).)
Top
Events
  NameDescription
Public eventDisposed
Occurs when the component is disposed by a call to the Dispose method.
(Inherited from Component.)
Top
Remarks

The MySqlDataAdapter does not automatically generate the SQL statements required to reconcile changes made to a DataSet with the associated instance of MySQL. However, you can create a MySqlCommandBuilder object to automatically generate SQL statements for single-table updates if you set the SelectCommand property of the MySqlDataAdapter. Then, any additional SQL statements that you do not set are generated by the MySqlCommandBuilder.

The MySqlCommandBuilder registers itself as a listener for RowUpdating events whenever you set the DataAdapter property. You can only associate one MySqlDataAdapter or MySqlCommandBuilder object with each other at one time.

To generate INSERT, UPDATE, or DELETE statements, the MySqlCommandBuilder uses the SelectCommand property to retrieve a required set of metadata automatically. If you change the SelectCommand after the metadata has is retrieved (for example, after the first update), you should call the [!:RefreshSchema] method to update the metadata.

The SelectCommand must also return at least one primary key or unique column. If none are present, an InvalidOperation exception is generated, and the commands are not generated.

The MySqlCommandBuilder also uses the Connection, CommandTimeout, and Transaction properties referenced by the SelectCommand. The user should call RefreshSchema if any of these properties are modified, or if the SelectCommand itself is replaced. Otherwise the InsertCommand, UpdateCommand, and DeleteCommand properties retain their previous values.

If you call Dispose, the MySqlCommandBuilder is disassociated from the MySqlDataAdapter, and the generated commands are no longer used.

Note Note
Caution must be used when using MySqlCOmmandBuilder on MySql 4.0 systems. With MySql 4.0, database/schema information is not provided to the connector for a query. This means that a query that pulls columns from two identically named tables in two or more different databases will not cause an exception to be thrown but will not work correctly. Even more dangerous is the situation where your select statement references database X but is executed in database Y and both databases have tables with similar layouts. This situation can cause unwanted changes or deletes. This note does not apply to MySQL versions 4.1 and later.
Examples
The following example uses the MySqlCommand, along MySqlDataAdapter and MySqlConnection, to select rows from a data source. The example is passed an initialized DataSet, a connection string, a query string that is a SQL SELECT statement, and a string that is the name of the database table. The example then creates a MySqlCommandBuilder.
public static DataSet SelectRows(string myConnection, string mySelectQuery, string myTableName)
{
MySqlConnection myConn = new MySqlConnection(myConnection);
MySqlDataAdapter myDataAdapter = new MySqlDataAdapter();
myDataAdapter.SelectCommand = new MySqlCommand(mySelectQuery, myConn);
MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);

myConn.Open();

DataSet ds = new DataSet();
myDataAdapter.Fill(ds, myTableName);

//code to modify data in DataSet here

//Without the MySqlCommandBuilder this line would fail
myDataAdapter.Update(ds, myTableName);

myConn.Close();

return ds;
}
See Also