Pustalorc.MySqlConnectorWrapper 4.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Pustalorc.MySqlConnectorWrapper --version 4.0.0                
NuGet\Install-Package Pustalorc.MySqlConnectorWrapper -Version 4.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Pustalorc.MySqlConnectorWrapper" Version="4.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Pustalorc.MySqlConnectorWrapper --version 4.0.0                
#r "nuget: Pustalorc.MySqlConnectorWrapper, 4.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Pustalorc.MySqlConnectorWrapper as a Cake Addin
#addin nuget:?package=Pustalorc.MySqlConnectorWrapper&version=4.0.0

// Install Pustalorc.MySqlConnectorWrapper as a Cake Tool
#tool nuget:?package=Pustalorc.MySqlConnectorWrapper&version=4.0.0                

MySql Database Wrapper NuGet

Library that wraps Connector/NET (aka: MySql.Data) and MySqlConnector in order to reduce duplicate code when interacting with the connectors.

Installation & Usage


Before you begin, please make sure that your current solution has installed the nuget package of this library. You can find this package here.

Configuration Setup

Firstly, you need a configuration for the connector to use. Without a configuration, the connector will not know what server to connect to, which database to use, which user to use, etc.

You will want to create a class and have it inherit the Interface IConnectorConfiguration. You are free to include extra information in this class to use, such as table names. For example:

public class DatabaseConfig : IConnectorConfiguration
{
    public string MySqlServerAddress => "127.0.0.1";
    public ushort MySqlServerPort => 3306;
    public string DatabaseUsername => "myUsername";
    public string DatabasePassword => "myPassword";
    public string DatabaseName => "myDatabase";
    public string ConnectionString => "";
}

Note: These are { get; } only properties, but you can make them { get; set; } if you wish to be able to modify them mid run-time, or if you wish to serialize them into a configuration file. ConnectionString is also available and used on the DbConnectionStringBuilder classes to build a full connection string with the provided details.

Wrapper setup

Once you have a configuration and you think you are ready to move on, you can then use the connector wrapping.

There are 2 default implementations of the wrapper, one for MySql.Data and one for MySqlConnector.

If either one of these doesn't fit your use case, you can always create your own by inheriting from DatabaseConnectorWrapper and overriding things as necessary.

Since both wrappers function the same and are abstract to the point where they are virtually identical in method calls, I'll be using the MySqlConnector wrapper:

public class MyDatabaseLayerClass
{
    private MySqlConnectorWrapper<DatabaseConfig> Database { get; }

    public MyDatabaseLayerClass(DatabaseConfig config)
    {
        Database = new MySqlConnectorWrapper<DatabaseConfig>(config);

        if (!Database.TestConnection(out var exception))
        {
            Log.Exception(exception);
            throw exception;
        }

        CheckCreateSchema();
    }

    private void CheckCreateSchema()
    {
        var output = Database.ExecuteQuery($"SHOW TABLES LIKE '{Configuration.TableName}';", EQueryType.Scalar);

        if (!output.IsResultNull()) return;

        Database.ExecuteQuery(new Query($"CREATE TABLE `{Configuration.TableName}` (`Id` INT NOT NULL, PRIMARY KEY (`Id`));"));
    }
}

As shown in the example above, you can execute queries in 2 ways, either by just providing the details of the query (the query string, the type of query, etc.) or constructing a Query class yourself with the same details.

The Query class holds basic information about a query. This includes the following:

  • A query string - This is the command that the underlying SQL server will execute.
  • The type of the query - This tells the wrapper if it should run the query as a non query, as a scalar query or as a reader.
  • A list of Parameters for the query - These are used in binding data to the query. The type for the parameters is an abstract class, so make sure to use the parameter types exposed by your connector.
  • A callback - This is ran after the query has fully executed, and passes the output, as well as the current DbConnection and DbTransaction for further query execution. The callback is useful when using asynchronous calls in a run and forget style.

And that's about it, you can run queries from here, or full transactions with multiple queries (for example creating a table and filling it with data, or doing a heavy operation on a DB where rolling back is needed in case of an error), or even add a caching layer to your database. The limit will be yours in here.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.0.0-dev.1 73 8/12/2024
5.0.3 316 3/11/2023
5.0.2 259 2/9/2023
5.0.1 349 12/6/2022
5.0.0 427 7/27/2022
4.1.2 440 6/5/2022
4.1.1 432 6/5/2022
4.1.0 482 3/7/2022
4.0.0 680 2/11/2022
3.0.2 347 12/17/2021
3.0.1 308 10/12/2021
3.0.0 327 9/24/2021
2.3.0 543 10/5/2020
2.2.1 515 7/9/2020
2.2.0 505 7/7/2020

Multiple changes, primarily the caching library was removed, and both separate versions for MySql.Data and MySqlConnector were merged.