Class MySqlConnection
Represents a connection to a MySQL database. This class cannot be inherited.
Inheritance
Implements
Namespace: MySql.Data.MySqlClient
Assembly: MySql.Data.dll
Version: 9.3.0
Syntax
public sealed class MySqlConnection : DbConnection, IComponent, IDbConnection, IDisposable, IAsyncDisposable, ICloneable
Remarks
A My
If the My
Constructors
MySqlConnection()
Initializes a new instance of the My
Declaration
public MySqlConnection()
Remarks
You can read more about it here.
MySqlConnection(String)
Initializes a new instance of the My
Declaration
public MySqlConnection(string connectionString)
Parameters
Type | Name | Description |
---|---|---|
System. |
connectionString | The connection properties used to open the MySQL database. |
Remarks
You can read more about it here.
Properties
ConnectionString
Gets or sets the string used to connect to a MySQL database.
Declaration
[Browsable(true)]
public override string ConnectionString { get; set; }
Property Value
Type | Description |
---|---|
System. |
Overrides
Remarks
You can read more about it here.
ConnectionTimeout
Gets the time to wait while trying to establish a connection before terminating the attempt and generating an error.
Declaration
[Browsable(true)]
public override int ConnectionTimeout { get; }
Property Value
Type | Description |
---|---|
System. |
Overrides
Remarks
A value of 0 indicates no limit, and should be avoided in a call to
Connection
Exceptions
Type | Condition |
---|---|
System. |
The value set is less than 0. |
Database
Gets the name of the current database or the database to be used after a connection is opened.
Declaration
[Browsable(true)]
public override string Database { get; }
Property Value
Type | Description |
---|---|
System. |
The name of the current database or the name of the database to be used after a connection is opened. The default value is an empty string. |
Overrides
Remarks
The Database property does not update dynamically.
If you change the current database using a SQL statement, then this property
may reflect the wrong value. If you change the current database using the Change
DataSource
Gets the name of the MySQL server to which to connect.
Declaration
[Browsable(true)]
public override string DataSource { get; }
Property Value
Type | Description |
---|---|
System. |
Overrides
DbProviderFactory
Gets the instance of the My
Declaration
protected override DbProviderFactory DbProviderFactory { get; }
Property Value
Type | Description |
---|---|
System. |
Overrides
IsDisposed
Declaration
[Browsable(true)]
public bool IsDisposed { get; }
Property Value
Type | Description |
---|---|
System. |
IsPasswordExpired
Gets a boolean value that indicates whether the password associated to the connection is expired.
Declaration
public bool IsPasswordExpired { get; }
Property Value
Type | Description |
---|---|
System. |
ServerThread
Returns the ID of the server thread this connection is executing on.
Declaration
[Browsable(false)]
public int ServerThread { get; }
Property Value
Type | Description |
---|---|
System. |
ServerVersion
Gets a string containing the version of the MySQL server to which the client is connected.
Declaration
[Browsable(false)]
public override string ServerVersion { get; }
Property Value
Type | Description |
---|---|
System. |
The version of the instance of MySQL. |
Overrides
Exceptions
Type | Condition |
---|---|
System. |
The connection is closed. |
State
Gets the current state of the connection.
Declaration
[Browsable(false)]
public override ConnectionState State { get; }
Property Value
Type | Description |
---|---|
System. |
A bitwise combination of the System. |
Overrides
Remarks
The allowed state changes are:
-
From System.
Data. to System.Connection State. Closed Data. , using the System.Connection State. Open Data. method of the connection object.Connection State. Open - From Open to Closed, using either the Close method or the Dispose method of the connection object.
UseCompression
Indicates if this connection should use compression when communicating with the server.
Declaration
[Browsable(false)]
public bool UseCompression { get; }
Property Value
Type | Description |
---|---|
System. |
Methods
BeginDbTransaction(IsolationLevel)
Starts a database transaction.
Declaration
protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
Parameters
Type | Name | Description |
---|---|---|
System. |
isolationLevel | Specifies the System. |
Returns
Type | Description |
---|---|
System. |
A My |
Overrides
Exceptions
Type | Condition |
---|---|
System. |
Parallel transactions are not supported. |
BeginDbTransactionAsync(IsolationLevel, CancellationToken)
Asynchronous version of Begin
Declaration
protected override async ValueTask<DbTransaction> BeginDbTransactionAsync(IsolationLevel isolationLevel, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System. |
isolationLevel | Specifies the System. |
System. |
cancellationToken | A token to cancel the asynchronous operation. |
Returns
Type | Description |
---|---|
System. |
A System.Threading.Tasks.ValueTask<TResult> representing the new transaction. |
Overrides
Exceptions
Type | Condition |
---|---|
System. |
Parallel transactions are not supported. |
BeginTransaction()
Begins a database transaction.
Declaration
public MySqlTransaction BeginTransaction()
Returns
Type | Description |
---|---|
My |
A My |
Exceptions
Type | Condition |
---|---|
System. |
Parallel transactions are not supported. |
BeginTransaction(IsolationLevel, String)
Starts a database transaction.
Declaration
public MySqlTransaction BeginTransaction(IsolationLevel isolationLevel, string scope = "")
Parameters
Type | Name | Description |
---|---|---|
System. |
isolationLevel | Specifies the System. |
System. |
scope | The scope of the transaction. |
Returns
Type | Description |
---|---|
My |
A My |
Exceptions
Type | Condition |
---|---|
System. |
Parallel transactions are not supported. |
BeginTransactionAsync()
Asynchronous version of Begin
Declaration
public ValueTask<MySqlTransaction> BeginTransactionAsync()
Returns
Type | Description |
---|---|
System. |
A My |
Exceptions
Type | Condition |
---|---|
System. |
Parallel transactions are not supported. |
BeginTransactionAsync(IsolationLevel)
Asynchronous version of Begin
Declaration
public ValueTask<MySqlTransaction> BeginTransactionAsync(IsolationLevel isolationLevel)
Parameters
Type | Name | Description |
---|---|---|
System. |
isolationLevel | Specifies the System. |
Returns
Type | Description |
---|---|
System. |
A System.Threading.Tasks.ValueTask<TResult> representing the new transaction. |
Exceptions
Type | Condition |
---|---|
System. |
Parallel transactions are not supported. |
BeginTransactionAsync(IsolationLevel, CancellationToken)
Asynchronous version of Begin
Declaration
public ValueTask<MySqlTransaction> BeginTransactionAsync(IsolationLevel isolationLevel, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System. |
isolationLevel | Specifies the System. |
System. |
cancellationToken | The cancellation token. |
Returns
Type | Description |
---|---|
System. |
A System.Threading.Tasks.ValueTask<TResult> representing the new transaction. |
Exceptions
Type | Condition |
---|---|
System. |
Parallel transactions are not supported. |
BeginTransactionAsync(CancellationToken)
Asynchronous version of Begin
Declaration
public ValueTask<MySqlTransaction> BeginTransactionAsync(CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System. |
cancellationToken | A token to cancel the asynchronous operation. |
Returns
Type | Description |
---|---|
System. |
A My |
Exceptions
Type | Condition |
---|---|
System. |
Parallel transactions are not supported. |
CancelQuery(Int32)
Cancels the query after the specified time interval.
Declaration
public void CancelQuery(int timeout)
Parameters
Type | Name | Description |
---|---|---|
System. |
timeout | The length of time (in seconds) to wait for the cancellation of the command execution. |
CancelQueryAsync(Int32, CancellationToken)
Asynchronous version of the Cancel
Declaration
public Task CancelQueryAsync(int timeout, CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
System. |
timeout | The length of time (in seconds) to wait for the cancellation of the command execution. |
System. |
cancellationToken | The cancellation token. |
Returns
Type | Description |
---|---|
System. |
ChangeDatabase(String)
Changes the current database for an open My
Declaration
public override void ChangeDatabase(string databaseName)
Parameters
Type | Name | Description |
---|---|---|
System. |
databaseName | The name of the database to use. |
Overrides
Remarks
The value supplied in the databaseName parameter must be a valid database name. The databaseName parameter cannot contain a null value, an empty string, or a string with only blank characters.
When you are using connection pooling against MySQL, and you close the connection, it is returned to the connection pool. The next time the connection is retrieved from the pool, the reset connection request executes before the user performs any operations.
Exceptions
Type | Condition |
---|---|
System. |
The database name is not valid. |
System. |
The connection is not open. |
My |
Cannot change the database. |
ChangeDatabaseAsync(String, CancellationToken)
Asynchronous version of the Change
Declaration
public override Task ChangeDatabaseAsync(string databaseName, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System. |
databaseName | The name of the database to use. |
System. |
cancellationToken | The cancellation token. |
Returns
Type | Description |
---|---|
System. |
A task representing the asynchronous operation. |
Overrides
ClearAllPools()
Clears all connection pools.
Declaration
public static void ClearAllPools()
Remarks
ClearAllPools essentially performs a Clear
ClearAllPoolsAsync(CancellationToken)
Asynchronous version of the Clear
Declaration
public Task ClearAllPoolsAsync(CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System. |
cancellationToken | The cancellation token. |
Returns
Type | Description |
---|---|
System. |
ClearPool(MySqlConnection)
Empties the connection pool associated with the specified connection.
Declaration
public static void ClearPool(MySqlConnection connection)
Parameters
Type | Name | Description |
---|---|---|
My |
connection | The My |
Remarks
Clear
ClearPoolAsync(MySqlConnection, CancellationToken)
Asynchronous version of the Clear
Declaration
public Task ClearPoolAsync(MySqlConnection connection, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
My |
connection | The connection associated with the pool to be cleared. |
System. |
cancellationToken | The cancellation token. |
Returns
Type | Description |
---|---|
System. |
Clone()
Creates a new My
Declaration
public object Clone()
Returns
Type | Description |
---|---|
System. |
A cloned My |
CloneWith(String)
Returns an unopened copy of this connection with a new connection string. If the Password
in connectionString
is not set, the password from this connection will be used.
This allows creating a new connection with the same security information while changing other options,
such as database or pooling.
Declaration
public MySqlConnection CloneWith(string connectionString)
Parameters
Type | Name | Description |
---|---|---|
System. |
connectionString | The new connection string to be used. |
Returns
Type | Description |
---|---|
My |
A new My |
Close()
Closes the connection to the database. This is the preferred method of closing any open connection.
Declaration
public override void Close()
Overrides
Remarks
The Close() method rolls back any pending transactions. It then releases the connection to the connection pool, or closes the connection if connection pooling is disabled.
An application can call Close() more than one time. No exception is generated.
CloseAsync()
Asynchronous version of the Close() method.
Declaration
public override Task CloseAsync()
Returns
Type | Description |
---|---|
System. |
Overrides
CreateCommand()
Creates and returns a My
Declaration
public MySqlCommand CreateCommand()
Returns
Type | Description |
---|---|
My |
A My |
CreateDbCommand()
Creates and returns a System.Data.Common.DbCommand object associated with the current connection.
Declaration
protected override DbCommand CreateDbCommand()
Returns
Type | Description |
---|---|
System. |
A System. |
Overrides
Dispose()
Releases the resources used by the My
Declaration
public void Dispose()
Dispose(Boolean)
Declaration
protected override void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
System. |
disposing |
Overrides
DisposeAsync()
Declaration
public override async ValueTask DisposeAsync()
Returns
Type | Description |
---|---|
System. |
Overrides
EnlistTransaction(Transaction)
Enlists in the specified transaction.
Declaration
public override void EnlistTransaction(Transaction transaction)
Parameters
Type | Name | Description |
---|---|---|
System. |
transaction | A reference to an existing System. |
Overrides
Finalize()
Declaration
protected void Finalize()
GetSchema()
Returns schema information for the data source of this System.
Declaration
public override DataTable GetSchema()
Returns
Type | Description |
---|---|
System. |
A System. |
Overrides
GetSchema(String)
Returns schema information for the data source of this
System.
Declaration
public override DataTable GetSchema(string collectionName)
Parameters
Type | Name | Description |
---|---|---|
System. |
collectionName | Specifies the name of the schema to return. |
Returns
Type | Description |
---|---|
System. |
A System. |
Overrides
GetSchema(String, String[])
Returns schema information for the data source of this System.
Declaration
public override DataTable GetSchema(string collectionName, string[] restrictionValues)
Parameters
Type | Name | Description |
---|---|---|
System. |
collectionName | Specifies the name of the schema to return. |
System. |
restrictionValues | Specifies a set of restriction values for the requested schema. |
Returns
Type | Description |
---|---|
System. |
A System. |
Overrides
GetSchemaAsync(String, String[], CancellationToken)
Asynchronous version of Get
Declaration
public Task<DataTable> GetSchemaAsync(string collectionName, string[] restrictionValues, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System. |
collectionName | Specifies the name of the schema to return. |
System. |
restrictionValues | Specifies a set of restriction values for the requested schema. |
System. |
cancellationToken | A token to cancel the asynchronous operation. |
Returns
Type | Description |
---|---|
System. |
A task representing the asynchronous operation. |
GetSchemaAsync(String, CancellationToken)
Asynchronous version of Get
Declaration
public Task<DataTable> GetSchemaAsync(string collectionName, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System. |
collectionName | Specifies the name of the schema to return. |
System. |
cancellationToken | A token to cancel the asynchronous operation. |
Returns
Type | Description |
---|---|
System. |
A task representing the asynchronous operation. |
GetSchemaAsync(CancellationToken)
Asynchronous version of Get
Declaration
public Task<DataTable> GetSchemaAsync(CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System. |
cancellationToken | A token to cancel the asynchronous operation. |
Returns
Type | Description |
---|---|
System. |
A task representing the asynchronous operation. |
GetSchemaCollection(String, String[])
Gets a schema collection based on the provided restriction values.
Declaration
public MySqlSchemaCollection GetSchemaCollection(string collectionName, string[] restrictionValues)
Parameters
Type | Name | Description |
---|---|---|
System. |
collectionName | The name of the collection. |
System. |
restrictionValues | The values to restrict. |
Returns
Type | Description |
---|---|
My |
A schema collection object. |
GetSchemaCollectionAsync(String, String[], CancellationToken)
Asynchronous version of the Get
Declaration
public Task<MySqlSchemaCollection> GetSchemaCollectionAsync(string collectionName, string[] restrictionValues, CancellationToken cancellationToken = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
System. |
collectionName | The name of the collection. |
System. |
restrictionValues | The values to restrict. |
System. |
cancellationToken | The cancellation token. |
Returns
Type | Description |
---|---|
System. |
A collection of schema objects. |
Open()
Opens a database connection with the property settings specified by the Connection
Declaration
public override void Open()
Overrides
Remarks
The My
Exceptions
Type | Condition |
---|---|
System. |
Cannot open a connection without specifying a data source or server. |
My |
A connection-level error occurred while opening the connection. |
OpenAsync(CancellationToken)
Declaration
public override Task OpenAsync(CancellationToken cancellationToken)
Parameters
Type | Name | Description |
---|---|---|
System. |
cancellationToken |
Returns
Type | Description |
---|---|
System. |
Overrides
Ping()
Pings the server.
Declaration
public bool Ping()
Returns
Type | Description |
---|---|
System. |
|
PingAsync()
Pings the server.
Declaration
public Task<bool> PingAsync()
Returns
Type | Description |
---|---|
System. |
|
Events
InfoMessage
Occurs when MySQL returns warnings as a result of executing a command or query.
Declaration
public event MySqlInfoMessageEventHandler InfoMessage
Event Type
Type | Description |
---|---|
My |
WebAuthnActionRequested
Occurs when WebAuthn authentication makes a request to perform the gesture action on a device.
Declaration
public event WebAuthnActionCallback WebAuthnActionRequested
Event Type
Type | Description |
---|---|
Web |