Top Related Projects
A pure node.js JavaScript Client implementing the MySQL protocol.
MySQL client library for Python
:zap: fast mysqljs/mysql compatible mysql driver for node.js
ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
Feature-rich ORM for modern Node.js and TypeScript, it supports PostgreSQL (with JSON and JSONB support), MySQL, MariaDB, SQLite, MS SQL Server, Snowflake, Oracle DB (v6), DB2 and DB2 for IBM i.
Quick Overview
MySqlConnector is a high-performance, asynchronous ADO.NET data provider for MySQL. It's designed to be a drop-in replacement for the official MySQL Connector/NET, offering improved performance and reliability. The library supports .NET Framework, .NET Core, and .NET Standard.
Pros
- Significantly faster performance compared to the official connector
- Full asynchronous support, including cancellation
- Better handling of connection pooling and timeouts
- Actively maintained with frequent updates and bug fixes
Cons
- May have some compatibility issues with legacy applications
- Less widespread adoption compared to the official connector
- Potential learning curve for developers familiar with the official connector
- Some advanced MySQL features might not be fully supported
Code Examples
- Establishing a connection and executing a query:
using MySqlConnector;
using var connection = new MySqlConnection("Server=localhost;Database=test;Uid=root;Pwd=password;");
await connection.OpenAsync();
using var command = new MySqlCommand("SELECT * FROM users WHERE id = @id", connection);
command.Parameters.AddWithValue("@id", 1);
using var reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
Console.WriteLine(reader.GetString("name"));
}
- Executing a non-query command:
using var connection = new MySqlConnection("Server=localhost;Database=test;Uid=root;Pwd=password;");
await connection.OpenAsync();
using var command = new MySqlCommand("INSERT INTO users (name, email) VALUES (@name, @email)", connection);
command.Parameters.AddWithValue("@name", "John Doe");
command.Parameters.AddWithValue("@email", "john@example.com");
int rowsAffected = await command.ExecuteNonQueryAsync();
Console.WriteLine($"Rows affected: {rowsAffected}");
- Using transactions:
using var connection = new MySqlConnection("Server=localhost;Database=test;Uid=root;Pwd=password;");
await connection.OpenAsync();
using var transaction = await connection.BeginTransactionAsync();
try
{
using var command = new MySqlCommand("UPDATE accounts SET balance = balance - @amount WHERE id = @fromId", connection, transaction);
command.Parameters.AddWithValue("@amount", 100);
command.Parameters.AddWithValue("@fromId", 1);
await command.ExecuteNonQueryAsync();
command.CommandText = "UPDATE accounts SET balance = balance + @amount WHERE id = @toId";
command.Parameters.AddWithValue("@toId", 2);
await command.ExecuteNonQueryAsync();
await transaction.CommitAsync();
}
catch
{
await transaction.RollbackAsync();
throw;
}
Getting Started
-
Install the NuGet package:
dotnet add package MySqlConnector
-
Add the following using statement to your C# file:
using MySqlConnector;
-
Create a connection string and establish a connection:
string connectionString = "Server=localhost;Database=test;Uid=root;Pwd=password;"; using var connection = new MySqlConnection(connectionString); await connection.OpenAsync();
-
You're now ready to execute commands and queries using the
MySqlCommand
class.
Competitor Comparisons
A pure node.js JavaScript Client implementing the MySQL protocol.
Pros of mysql
- JavaScript-based, ideal for Node.js applications
- Lightweight and easy to use for simple database operations
- Large community and extensive documentation
Cons of mysql
- Limited support for advanced MySQL features
- Performance may be slower for complex queries or large datasets
- Lacks built-in connection pooling
Code Comparison
MySqlConnector (.NET):
using var connection = new MySqlConnection(connectionString);
await connection.OpenAsync();
using var command = new MySqlCommand("SELECT * FROM users", connection);
using var reader = await command.ExecuteReaderAsync();
mysql (Node.js):
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'user',
password: 'password',
database: 'mydb'
});
connection.query('SELECT * FROM users', (error, results, fields) => {
if (error) throw error;
console.log(results);
});
The MySqlConnector example demonstrates async/await usage and stronger typing, while the mysql example shows a more callback-based approach typical in Node.js. MySqlConnector offers better performance and more advanced features, but mysql is simpler to use for basic operations in JavaScript environments.
MySQL client library for Python
Pros of PyMySQL
- Written in pure Python, making it easy to install and use across different platforms
- Supports both Python 2 and Python 3, providing broader compatibility
- Lightweight and simple to use, with a straightforward API
Cons of PyMySQL
- Generally slower performance compared to MySqlConnector
- Lacks some advanced features like connection pooling and asynchronous operations
- May have limited support for newer MySQL features
Code Comparison
PyMySQL:
import pymysql
connection = pymysql.connect(host='localhost', user='user', password='password', database='db')
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM table")
result = cursor.fetchall()
MySqlConnector:
using MySqlConnector;
using var connection = new MySqlConnection("Server=localhost;User ID=user;Password=password;Database=db");
connection.Open();
using var command = new MySqlCommand("SELECT * FROM table", connection);
using var reader = command.ExecuteReader();
Both libraries provide similar functionality for connecting to MySQL databases, but MySqlConnector offers better performance and more advanced features for .NET applications, while PyMySQL is a simpler, pure Python solution that works across different Python versions.
:zap: fast mysqljs/mysql compatible mysql driver for node.js
Pros of node-mysql2
- Supports both Promise-based and callback-style APIs, offering flexibility for different coding styles
- Includes built-in connection pooling, simplifying database connection management
- Provides a streaming query interface, allowing efficient handling of large datasets
Cons of node-mysql2
- Limited to Node.js environments, whereas MySqlConnector supports multiple .NET platforms
- May have slightly lower performance in some scenarios compared to MySqlConnector's optimized implementation
- Less extensive documentation and community support compared to MySqlConnector
Code Comparison
MySqlConnector (.NET):
using var connection = new MySqlConnection(connectionString);
await connection.OpenAsync();
using var command = new MySqlCommand("SELECT * FROM users", connection);
using var reader = await command.ExecuteReaderAsync();
node-mysql2 (Node.js):
const mysql = require('mysql2/promise');
const connection = await mysql.createConnection(connectionString);
const [rows, fields] = await connection.execute('SELECT * FROM users');
Both libraries offer async/await support and similar query execution patterns. MySqlConnector integrates seamlessly with .NET's ADO.NET model, while node-mysql2 provides a more JavaScript-friendly API with destructuring and Promises.
ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
Pros of TypeORM
- Supports multiple databases, not limited to MySQL
- Provides ORM features like entity relationships and migrations
- Offers both Active Record and Data Mapper patterns
Cons of TypeORM
- Steeper learning curve due to more complex features
- May have performance overhead compared to raw SQL queries
- Requires TypeScript for optimal usage
Code Comparison
MySqlConnector:
using (var connection = new MySqlConnection(connectionString))
{
connection.Open();
var command = new MySqlCommand("SELECT * FROM users", connection);
var reader = command.ExecuteReader();
}
TypeORM:
import { createConnection } from "typeorm";
import { User } from "./entity/User";
createConnection().then(async connection => {
const userRepository = connection.getRepository(User);
const users = await userRepository.find();
});
Summary
MySqlConnector is a lightweight, high-performance MySQL-specific connector for .NET, while TypeORM is a feature-rich ORM for TypeScript and JavaScript that supports multiple databases. TypeORM offers more advanced features like entity relationships and migrations but may have a steeper learning curve. MySqlConnector provides direct SQL access with potentially better performance for MySQL-specific applications.
Feature-rich ORM for modern Node.js and TypeScript, it supports PostgreSQL (with JSON and JSONB support), MySQL, MariaDB, SQLite, MS SQL Server, Snowflake, Oracle DB (v6), DB2 and DB2 for IBM i.
Pros of Sequelize
- Supports multiple databases (MySQL, PostgreSQL, SQLite, etc.)
- Provides an ORM with model definitions and associations
- Offers advanced features like migrations and seeders
Cons of Sequelize
- Steeper learning curve due to its extensive feature set
- May introduce performance overhead for complex queries
- Requires more setup and configuration compared to MySqlConnector
Code Comparison
MySqlConnector:
using (var connection = new MySqlConnection(connectionString))
{
connection.Open();
using (var command = new MySqlCommand("SELECT * FROM users", connection))
using (var reader = command.ExecuteReader())
{
// Process results
}
}
Sequelize:
const User = sequelize.define('User', {
// Model attributes
});
const users = await User.findAll();
// Process results
Summary
MySqlConnector is a lightweight, high-performance MySQL driver for .NET, while Sequelize is a feature-rich ORM for Node.js supporting multiple databases. MySqlConnector offers simplicity and raw SQL execution, whereas Sequelize provides abstraction and database-agnostic operations. Choose MySqlConnector for .NET projects requiring direct MySQL access, and Sequelize for Node.js applications needing ORM capabilities across various databases.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
MySQL Connector for .NET and .NET Core
This is an ADO.NET data
provider for MySQL and other compatible servers including MariaDB.
It provides implementations of
DbConnection
, DbCommand
, DbDataReader
, DbTransaction
âthe classes
needed to query and update databases from managed code.
Complete documentation is available at the MySqlConnector Documentation Website.
Why Use This Library?
Performance
This library outperforms MySQL Connector/NET (MySql.Data
) on benchmarks:
(Client: MySqlConnector 2.3.1, MySql.Data 8.2.0, Ubuntu 23.04, .NET 8.0; Server: Azure Database for MySQL 8.0.34, TLS 1.2)
Server Compatibility
This library is compatible with many MySQL-compatible servers, including MySQL 5.5 and newer and MariaDB 10.x and newer. MySql.Data only supports MySQL Server.
Bug Fixes
This library fixes dozens of outstanding bugs in Connector/NET.
Cutting Edge
This library implements the latest ADO.NET APIs, from async (introduced in .NET Framework 4.5), through DbBatch
(.NET 6.0) and DbDataSource
(.NET 7.0).
License
This library is MIT-licensed and may be freely distributed with commercial software. Commercial software that uses Connector/NET may have to purchase a commercial license from Oracle.
Related Projects
This library is compatible with popular .NET ORMs including:
- Dapper (GitHub, NuGet)
- FreeSql (GitHub, NuGet)
- LINQ to DB (GitHub, NuGet) including ClickHouse support
- NHibernate (GitHub, NuGet)
- NReco.Data (GitHub, NuGet)
- Paradigm ORM (GitHub, NuGet)
- RepoDb (GitHub, NuGet)
- ServiceStack.OrmLite (GitHub, NuGet)
- SimpleStack.Orm (GitHub, NuGet)
For Entity Framework support, use:
For ASP.NET Core health checks, use:
Build Status
Building
Install the latest .NET.
To build and run the tests, clone the repo and execute:
dotnet restore
dotnet test tests\MySqlConnector.Tests
To run the integration tests, see the instructions.
Goals
The goals of this project are:
- .NET Standard support: It must run on the full .NET Framework and all platforms supported by .NET Core.
- Async: All operations must be truly asynchronous whenever possible.
- High performance: Avoid unnecessary allocations and copies when reading data.
- Lightweight: Only the core of ADO.NET is implemented, not EF or Designer types.
- Managed: Managed code only, no native code.
- Independent: This is a clean-room reimplementation of the MySQL Protocol, not based on Connector/NET.
Cloning the full API of Connector/NET is not a goal of this project, although it will try not to be gratuitously incompatible. For typical scenarios, migrating to this package should be easy.
License
This library is licensed under the MIT License.
Contributing
If you'd like to contribute to MySqlConnector, please read our contributing guidelines.
Acknowledgements
Development of MySqlConnector is supported by:
Top Related Projects
A pure node.js JavaScript Client implementing the MySQL protocol.
MySQL client library for Python
:zap: fast mysqljs/mysql compatible mysql driver for node.js
ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
Feature-rich ORM for modern Node.js and TypeScript, it supports PostgreSQL (with JSON and JSONB support), MySQL, MariaDB, SQLite, MS SQL Server, Snowflake, Oracle DB (v6), DB2 and DB2 for IBM i.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot