MySQL · Retrieve

Every data type, written and read back

What each MySQL type reads back as, in one scenario: a row of data_type_showcase is written with Create and read with Retrieve, and each attribute's .NET type is printed.

Set up for this page

The scenarios build on the sample application: SampleContext.cs (the dbContext every sample is given); SampleOutput.cs (Step and Show, which print); SampleContext.Application.cs (dbContext.Service, the application's ARX service); SampleCatalog.cs (the tables' ids, keys, lookups and display names).

1 Written with Create, read with Retrieve

Write one value of each type, read the row, print each attribute's type and value.

Already in scope: service (ArxMySqlDbService)

C#
var row = new ArxEntity("data_type_showcase");
row["text_value"] = "Read back R641";
row["char_value"] = "AB";
row["smallint_value"] = (short)7;
row["int_value"] = 42;
row["bigint_value"] = 9_000_000_000L;
row["unsigned_value"] = 4_000_000_000u;
row["decimal_value"] = 12345.678901m;
row["float_value"] = 1.5f;
row["double_value"] = 2.25d;
row["bool_value"] = true;
row["uuid_value"] = Guid.Parse("6c1b3a6e-7c2d-4c7e-9a51-2a9c6b0e1641");
row["timestamp_value"] = new DateTimeOffset(2026, 4, 1, 11, 30, 0, TimeSpan.FromHours(2));
row["datetime_value"] = new DateTime(2026, 4, 1, 9, 30, 0);
row["date_value"] = new DateOnly(2026, 4, 1);
row["time_value"] = new TimeOnly(9, 30);
row["blob_value"] = new byte[] { 1, 2, 3 };
row["json_value"] = new ArxJsonB { SerializedValue = "{\"tier\": \"gold\"}" };
row["mood"] = new ArxSelectionSetValue("Mood", "happy");
var id = await service.CreateAsync(row);

var read = await service.RetrieveAsync("data_type_showcase", id) ?? throw new InvalidOperationException("The data_type_showcase record isn't there.");

foreach (var column in new[] { "text_value", "char_value", "smallint_value", "int_value", "bigint_value", "unsigned_value", "decimal_value", "float_value",
             "double_value", "bool_value", "uuid_value", "timestamp_value", "datetime_value", "date_value", "time_value",
             "blob_value", "json_value", "mood" })
{
    var value = read[column];
    var text = value switch
    {
        ArxJsonB json => json.SerializedValue,
        DateTime dateTime => $"{dateTime:yyyy-MM-dd HH:mm} ({dateTime.Kind})",
        DateTimeOffset offset => $"{offset:yyyy-MM-dd HH:mm zzz}",
        Array array => "[" + string.Join(", ", array.Cast<object>()) + "]",
        _ => value?.ToString()
    };

    Console.WriteLine($"{column,-18} {value?.GetType().Name,-10} {text}");
}

Check it in the database

SQL

Run it in any SQL tool connected to the database, after the code has run.

SET time_zone = '+00:00';
SELECT char_value, date_value, time_value, timestamp_value, json_value, mood
FROM data_type_showcase
WHERE text_value = 'Read back R641';

Expect: 1 row, AB (CHAR(3) drops its padding when read), 2026-04-01, 09:30:00.000000, 2026-04-01 09:30:00.000000 (the TIMESTAMP's instant in UTC), {"tier": "gold"}, happy.

Run the complete sample

The code above comes from one program, built in a fresh project and run against the database. To run it yourself, here is every file and step.

The program, its files and how to run it

Run it locally

  1. .NET SDK: .NET SDK 10.0.100 or later 10.0.x (target net10.0).
  2. Create the project:
    Terminal
    dotnet new console -n ArxSample -f net10.0
    cd ArxSample
  3. Install the packages:
    Terminal
    dotnet add package ARX.SDK.MySQL --version 1.0.0-rc.1
  4. Database: a MySQL 8.4 (tested with 8.4.13) database with the sample schema.
  5. Configuration: set ARX_SAMPLES_MYSQL to its connection string. Nothing is hard-coded.
  6. Source code: add the files of the sample application this sample uses (SampleContext.cs, SampleOutput.cs, SampleContext.Application.cs, SampleCatalog.cs), then replace Program.cs and add the other file below, with the file names shown.
  7. Run it:
    Terminal
    dotnet run
  8. Expected output: below.

The files

Program.cs

Runs the sample. Generated for this page; it reads the connection string from the environment.

using ArxSamples.MySQL;
using ArxSamples.MySQL.Operations.Retrieve.RealCases;

// The connection string comes from the environment, never from the code (see "Configure the connection").
var connectionString = Environment.GetEnvironmentVariable("ARX_SAMPLES_MYSQL")
    ?? throw new InvalidOperationException("Set the ARX_SAMPLES_MYSQL environment variable to your MySQL connection string.");

await new RealCase1_EveryDataTypeReadBack().RunAsync(new SampleContext(connectionString));
RealCase1_EveryDataTypeReadBack.cs

The sample, as it is in the repository.

using ARX.SDK.Models;
using ARX.SDK.Services;
using static ArxSamples.MySQL.SampleOutput;

namespace ArxSamples.MySQL.Operations.Retrieve.RealCases;

/// <summary>
/// What each MySQL type reads back as, in one scenario: a row of data_type_showcase is written with
/// Create and read with Retrieve, and each attribute's .NET type is printed.
/// </summary>
public sealed class RealCase1_EveryDataTypeReadBack : ISample
{
    public string Name => "retrieve-real-1-every-data-type-read-back";

    public string Title => "Every data type, written and read back";

    public async Task RunAsync(SampleContext dbContext)
    {
        await Scenario1_WriteThenReadEveryTypeAsync(dbContext);
    }

    /// <summary>1. Write one value of each type, read the row, print each attribute's type and value.</summary>
    private static async Task Scenario1_WriteThenReadEveryTypeAsync(SampleContext dbContext)
    {
        Step("1. Written with Create, read with Retrieve");

        var row = new ArxEntity("data_type_showcase");
        row["text_value"] = "Read back R641";
        row["char_value"] = "AB";
        row["smallint_value"] = (short)7;
        row["int_value"] = 42;
        row["bigint_value"] = 9_000_000_000L;
        row["unsigned_value"] = 4_000_000_000u;
        row["decimal_value"] = 12345.678901m;
        row["float_value"] = 1.5f;
        row["double_value"] = 2.25d;
        row["bool_value"] = true;
        row["uuid_value"] = Guid.Parse("6c1b3a6e-7c2d-4c7e-9a51-2a9c6b0e1641");
        row["timestamp_value"] = new DateTimeOffset(2026, 4, 1, 11, 30, 0, TimeSpan.FromHours(2));
        row["datetime_value"] = new DateTime(2026, 4, 1, 9, 30, 0);
        row["date_value"] = new DateOnly(2026, 4, 1);
        row["time_value"] = new TimeOnly(9, 30);
        row["blob_value"] = new byte[] { 1, 2, 3 };
        row["json_value"] = new ArxJsonB { SerializedValue = "{\"tier\": \"gold\"}" };
        row["mood"] = new ArxSelectionSetValue("Mood", "happy");
        var id = await dbContext.Service.CreateAsync(row);

        var read = await dbContext.Service.RetrieveAsync("data_type_showcase", id) ?? throw new InvalidOperationException("The data_type_showcase record isn't there.");

        foreach (var column in new[] { "text_value", "char_value", "smallint_value", "int_value", "bigint_value", "unsigned_value", "decimal_value", "float_value",
                     "double_value", "bool_value", "uuid_value", "timestamp_value", "datetime_value", "date_value", "time_value",
                     "blob_value", "json_value", "mood" })
        {
            var value = read[column];
            var text = value switch
            {
                ArxJsonB json => json.SerializedValue,
                DateTime dateTime => $"{dateTime:yyyy-MM-dd HH:mm} ({dateTime.Kind})",
                DateTimeOffset offset => $"{offset:yyyy-MM-dd HH:mm zzz}",
                Array array => "[" + string.Join(", ", array.Cast<object>()) + "]",
                _ => value?.ToString()
            };

            Show($"{column,-18} {value?.GetType().Name,-10} {text}");
        }

        /* ===========================================================
         * [DIRECT QUERY CHECK]
         * -----------------------------------------------------------
         * SET time_zone = '+00:00';
         * SELECT char_value, date_value, time_value, timestamp_value, json_value, mood
         * FROM data_type_showcase
         * WHERE text_value = 'Read back R641';
         * -- Expect: 1 row, AB (CHAR(3) drops its padding when read), 2026-04-01, 09:30:00.000000, 2026-04-01 09:30:00.000000
         * --         (the TIMESTAMP's instant in UTC), {"tier": "gold"}, happy.
         * ===========================================================
         */
    }
}

Expected output

What dotnet run printed when the sample was verified, on a freshly created sample schema. Ids differ when the tables already hold rows.

Output
  > 1. Written with Create, read with Retrieve
      text_value         String     Read back R641
      char_value         String     AB
      smallint_value     Int16      7
      int_value          Int32      42
      bigint_value       Int64      9000000000
      unsigned_value     UInt32     4000000000
      decimal_value      Decimal    12345.678901
      float_value        Single     1.5
      double_value       Double     2.25
      bool_value         Boolean    True
      uuid_value         Guid       6c1b3a6e-7c2d-4c7e-9a51-2a9c6b0e1641
      timestamp_value    DateTime   2026-04-01 09:30 (Utc)
      datetime_value     DateTime   2026-04-01 09:30 (Unspecified)
      date_value         DateOnly   01/04/2026
      time_value         TimeOnly   9:30 am
      blob_value         Byte[]     [1, 2, 3]
      json_value         String     {"tier": "gold"}
      mood               String     happy