MySQL: Online
Load: Low
Sandbox: Running

Using NuGet Packages and a Database in a CounterStrikeSharp Plugin

Many useful plugins store data: bans, stats, ranks. For that you add a NuGet package to the project, and the most important rule is not to block the game while you wait for the database. This guide covers both, using Dapper and MySqlConnector as the example.

1

Add the packages

NuGet packages are listed in the project file. Dapper makes queries short and MySqlConnector talks to MariaDB and MySQL. Our compiler downloads them for you when it builds.

<ItemGroup>
  <PackageReference Include="CounterStrikeSharp.API" Version="1.0.*" />
  <PackageReference Include="Dapper" Version="2.*" />
  <PackageReference Include="MySqlConnector" Version="2.*" />
</ItemGroup>
2

What happens when you compile

The build produces your plugin DLL and, next to it, the DLLs of the packages your plugin needs. Files that CounterStrikeSharp already contains are separated into another folder, so you do not copy them twice.

3

What to copy to the server

Copy the plugin folder, with the extra package DLLs inside it, to the plugins folder of your server. Do not copy the dependencies folder, because the server already has those files and a second copy can cause conflicts.

YourPlugin-Release.zip
YourPlugin
YourPlugin.dll
Dapper.dll
MySqlConnector.dll
dependencies
Files already provided by CounterStrikeSharp
4

Run a query without freezing the game

The game runs on one main thread. If you wait for a database there, the whole server stutters. Run the query in Task.Run, and when you need to touch the game again, return to the main thread with Server.NextFrame.

using CounterStrikeSharp.API;
using Dapper;
using Microsoft.Extensions.Logging;
using MySqlConnector;

public void CountPlayers()
{
    Task.Run(async () =>
    {
        await using var connection = new MySqlConnection(Config.ConnectionString);        var count = await connection.ExecuteScalarAsync<int>("SELECT COUNT(*) FROM players");
        Server.NextFrame(() =>        {
            Logger.LogInformation("Players in the database: {Count}", count);
        });
    });
}
5

Keep the connection string in the config

Do not write the address, user and password in the code. Put the connection string in the plugin config, as in our config guide, so each server owner uses their own database.

6

Packages that do not belong in a plugin

Some packages are too big or are not made to live inside a game server, for example web frameworks such as ASP.NET. Prefer small libraries, and if a package fails to load, check whether it needs native files that are missing.

Do not use a database account that has more rights than the plugin needs. Create a user that can only touch its own tables.
7

Test before going live

Try the plugin on a test server with a test database first. Check that the tables are created, that a failed connection does not crash the plugin, and that the log gives a clear message when the database is down.

Was this tutorial useful?

Page generated in 0.011 seconds
CounterStrikeSharp Web Compiler V1.0 - Made with by RomaniaBarlap for the community.