Automatically generates single-table commands used to reconcile changes made to a DataSet with the associated MySQL database. This class cannot be inherited.
The 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 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 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.
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.
The following example uses the , along
and , to
select rows from a data source. The example is passed an initialized
, 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 Shared Function SelectRows(myConnection As String, mySelectQuery As String, myTableName As String) As DataSet
Dim myConn As New MySqlConnection(myConnection)
Dim myDataAdapter As New MySqlDataAdapter()
myDataAdapter.SelectCommand = New MySqlCommand(mySelectQuery, myConn)
Dim cb As SqlCommandBuilder = New MySqlCommandBuilder(myDataAdapter)
myConn.Open()
Dim ds As DataSet = 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()
End Function 'SelectRows
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;
}
Initializes a new instance of the class.
Initializes a new instance of the class
with the associated object.
The to use.
The registers itself as a listener for
events that are generated by the
specified in this property.
When you create a new instance MySqlCommandBuilder, any existing
MySqlCommandBuilder associated with this MySqlDataAdapter
is released.
Gets or sets a object for which SQL statements are automatically generated.
A object.
The registers itself as a listener for
events that are generated by the
specified in this property.
When you create a new instance MySqlCommandBuilder, any existing
MySqlCommandBuilder associated with this MySqlDataAdapter
is released.
Gets or sets the beginning character or characters to use when specifying MySQL
database objects (for example, tables or columns) whose names contain
characters such as spaces or reserved tokens.
The beginning character or characters to use. The default value is `.
Database objects in MySQL can contain special characters such as spaces that would
make normal SQL strings impossible to correctly parse. Use of the QuotePrefix
and the properties allows the
to build SQL commands that handle this situation.
Gets or sets the beginning character or characters to use when specifying MySQL
database objects (for example, tables or columns) whose names contain
characters such as spaces or reserved tokens.
The beginning character or characters to use. The default value is `.
Database objects in MySQL can contain special characters such as spaces that would
make normal SQL strings impossible to correctly parse. Use of the
and the QuoteSuffix properties allows the
to build SQL commands that handle this situation.
Gets the automatically generated object
required to perform deletions on the database.
The object generated to handle delete operations.
An application can use the GetDeleteCommand method for informational
or troubleshooting purposes because it returns the
object to be executed.
You can also use GetDeleteCommand as the basis of a modified command.
For example, you might call GetDeleteCommand and modify the
value, and then explicitly set that on the
.
After the SQL statement is first generated, the application must explicitly
call if it changes the statement in any way.
Otherwise, the GetDeleteCommand will be still be using information
from the previous statement, which might not be correct. The SQL statements
are first generated either when the application calls
or GetDeleteCommand.
Gets the automatically generated object
required to perform insertions on the database.
The object generated to handle insert operations.
An application can use the GetInsertCommand method for informational
or troubleshooting purposes because it returns the
object to be executed.
You can also use the GetInsertCommand as the basis of a modified command.
For example, you might call GetInsertCommand and modify the
value, and then explicitly set that on the
.
After the SQL statement is first generated, the application must explicitly
call if it changes the statement in any way.
Otherwise, the GetInsertCommand will be still be using information
from the previous statement, which might not be correct. The SQL statements
are first generated either when the application calls
or GetInsertCommand.
Gets the automatically generated object
required to perform updates on the database.
The object generated to handle update operations.
An application can use the GetUpdateCommand method for informational
or troubleshooting purposes because it returns the
object to be executed.
You can also use GetUpdateCommand as the basis of a modified command.
For example, you might call GetUpdateCommand and modify the
value, and then explicitly set that on the
.
After the SQL statement is first generated, the application must explicitly
call if it changes the statement in any way.
Otherwise, the GetUpdateCommand will be still be using information
from the previous statement, which might not be correct. The SQL statements
are first generated either when the application calls
or GetUpdateCommand.
Refreshes the database schema information used to generate INSERT, UPDATE, or
DELETE statements.
An application should call RefreshSchema whenever the SELECT statement
associated with the changes.
An application should call RefreshSchema whenever the
value of the changes.