MySQL Connector for .NET

repository·master·Indexed 23 days ago

https://github.com/mysql-net/mysqlconnector

A high-performance, fully asynchronous ADO.NET data provider for MySQL and MariaDB. It is a clean-room implementation of the MySQL protocol designed to outperform the official MySQL Connector/NET, providing managed code implementations of core ADO.NET classes like DbConnection, DbCommand, DbDataReader, and DbTransaction without relying on native dependencies.

Tokens
122.7K
Snippets
597
Records
892
Agent score
80%

What's inside MySqlConnector

  1. Overview of MySQL Connector for .NET

    master

    MySQL Connector for .NET is an ADO.NET data provider for MySQL and compatible servers like MariaDB. It provides the standard implementations of the core ADO.NET classes required to query and update databases from managed code:

    • DbConnection
    • DbCommand
    • DbDataReader
    • DbTransaction

    It is designed to be high-performance, fully asynchronous, and lightweight, implementing the MySQL Protocol in managed code without relying on native dependencies.

  2. Compatible .NET ORMs

    master

    MySqlConnector is compatible with several popular .NET Object-Relational Mappers (ORMs). Depending on your architectural needs, you can use the following libraries:

    • Dapper: A high-performance micro-ORM.
    • FreeSql: A lightweight ORM.
    • Gedaq: An ORM designed for MySqlConnector.
    • LINQ to DB: Supports LINQ queries and includes ClickHouse support.
    • MuchAdo: A data access library.
    • NHibernate: A mature, feature-rich ORM.
    • NReco.Data: A lightweight data access component.
    • SimpleStack.Orm: A simple ORM implementation.
  3. Understand MySQL column types for date storage

    master

    When storing .NET date values, choose between TIMESTAMP and DATETIME columns based on your requirements:

    • TIMESTAMP:
      • Range: 1970-01-01 00:00:01 to 2038-01-19 03:14:07.
      • Behavior: Values are converted to UTC for storage and from UTC for retrieval. This can lead to unexpected values if time zone handling is not managed.
    • DATETIME:
      • Range: 1000-01-01 to 9999-12-31.
      • Behavior: Stores the date and time as provided without automatic UTC conversion.

    Consult the official MySQL documentation for detailed behavior regarding these types.

  4. Understand MySqlConnector spans

    master

    All spans produced by MySqlConnector use ActivityKind.Client. The following spans are created during common operations:

    Span nameCreated byNotes
    OpenMySqlConnection.Open() and OpenAsync()Covers connection establishment or pool checkout.
    ExecuteMySqlCommand.Execute*() and MySqlBatch.Execute*()Begins when the command is sent and ends when the MySqlDataReader is closed or disposed.
    CommitMySqlTransaction.Commit() and CommitAsync()Covers the transaction commit round-trip.
    RollbackMySqlTransaction.Rollback() and RollbackAsync()Covers explicit or implicit rollback.
  5. Avoid precision loss with FLOAT columns

    master

    MySQL FLOAT columns use 32-bit single-precision IEEE 754 values. When retrieving these values, MySQL may use the FLT_DIG constant, which can cause an apparent loss of precision in the least-significant digit.

    To retrieve exact values without precision loss, use one of the following:

    1. Use the DOUBLE column type.
    2. Coerce the value to double-precision in your SQL query (e.g., SELECT value+0).
    3. Use a prepared statement via MySqlCommand.Prepare, which utilizes the binary protocol to retrieve the original value.
  6. W3C Trace Context Propagation to MySQL Server

    master

    When an Execute span is active and the .NET Activity uses W3C ID format, MySqlConnector attempts to send the active trace context to the MySQL Server as query attributes. This requires the server to support query attributes.

    Attributes sent:

    • traceparent: The W3C traceparent value from Activity.Id.
    • tracestate: The W3C tracestate value (if non-empty).

    Note: If these attributes are already manually provided in MySqlCommand.Attributes, MySqlConnector will not add duplicates.

  7. Configure load balancing for multiple servers

    master

    When the Server option contains a comma-delimited list of host names, you can use the LoadBalance option to control how connections are distributed.

    Note: RoundRobin and LeastConnections require Pooling=True. Random and FailOver can be used with Pooling=False.

    Available strategies:

    • RoundRobin (Default): Each new connection opened for the pool uses the next host name sequentially with wraparound.
    • FailOver: Each new connection tries the first host; subsequent hosts are used only if the first fails.
    • Random: Servers are tried in a random order.
    • LeastConnections: Servers are tried in ascending order of the number of currently-open connections in the pool.