App in C# (.NET)

    Note

    The article is being updated.

    To interact with YDB, create an instance of the driver, client, and session:

    • The YDB driver lets the app and YDB interact at the transport layer. The driver must exist throughout the YDB access lifecycle and be initialized before creating a client or session.
    • The YDB client runs on top of the YDB driver and enables the handling of entities and transactions.
    • The YDB session contains information about executed transactions and prepared queries, and is part of the YDB client context.

    App code snippet for driver initialization:

    App code snippet for creating a session:

    C# (.NET) - 图2

    • Series
    • Seasons
    • Episodes

    Once the tables are created, the method for getting information about data schema objects is called and the result of its execution is output.

    To create tables, use the session.ExecuteSchemeQuery method with a DDL (Data Definition Language) YQL query.

    Adding data to the created tables using an UPSERT statement of . A data update request is sent within a single request to the server with transaction auto-commit mode enabled.

    Code snippet for inserting and updating data:

    1. var response = await tableClient.SessionExec(async session =>
    2. {
    3. var query = @"
    4. DECLARE $id AS Uint64;
    5. DECLARE $title AS Utf8;
    6. DECLARE $release_date AS Date;
    7. UPSERT INTO series (series_id, title, release_date) VALUES
    8. ($id, $title, $release_date);
    9. ";
    10. return await session.ExecuteDataQuery(
    11. txControl: TxControl.BeginSerializableRW().Commit(),
    12. parameters: new Dictionary<string, YdbValue>
    13. {
    14. { "$id", YdbValue.MakeUint64(1) },
    15. { "$title", YdbValue.MakeUtf8("NewTitle") },
    16. { "$release_date", YdbValue.MakeDate(DateTime.UtcNow) }
    17. }
    18. );
    19. });

    C# (.NET) - 图4

    PRAGMA TablePathPrefix adds a specified prefix to the database table paths. It uses standard file system path concatenation: i.e., it supports parent folder referencing and requires no trailing slash. For example:

    For more information about PRAGMA YQL, see the YQL documentation.

    Retrieving data using a statement in YQL. Handling the retrieved data selection in the app.

    To execute YQL queries, use the Session.executeDataQuery() method. The SDK lets you explicitly control the execution of transactions and configure the transaction execution mode using the TxControl class. In the code snippet below, a transaction with the SerializableRW mode and an automatic commit after executing the request is used. The values of the request parameters are passed in the form of a dictionary name-value in the parameters argument.

    1. var response = await tableClient.SessionExec(async session =>
    2. {
    3. var query = @"
    4. DECLARE $id AS Uint64;
    5. SELECT
    6. series_id,
    7. title,
    8. release_date
    9. FROM series
    10. WHERE series_id = $id;
    11. ";
    12. return await session.ExecuteDataQuery(
    13. query: query,
    14. txControl: TxControl.BeginSerializableRW().Commit(),
    15. parameters: new Dictionary<string, YdbValue>
    16. {
    17. { "$id", YdbValue.MakeUint64(id) }
    18. },
    19. });
    20. response.Status.EnsureSuccess();
    21. var queryResponse = (ExecuteDataQueryResponse)response;
    22. var resultSet = queryResponse.Result.ResultSets[0];

    The result of query execution (resultset) consists of an organized set of rows. Example of processing the query execution result:

    C# (.NET) - 图7

    Making a that results in a data stream. Streaming lets you read an unlimited number of rows and amount of data.

    1. public void executeScanQuery()
    2. {
    3. var scanStream = TableClient.ExecuteScanQuery(@$"
    4. SELECT series_id, season_id, COUNT(*) AS episodes_count
    5. FROM episodes
    6. GROUP BY series_id, season_id
    7. ORDER BY series_id, season_id;
    8. ");
    9. while (await scanStream.Next())
    10. {
    11. scanStream.Response.EnsureSuccess();
    12. var resultSet = scanStream.Response.Result.ResultSetPart;
    13. if (resultSet != null)
    14. {
    15. foreach (var row in resultSet.Rows)
    16. {
    17. Console.WriteLine($"> ScanQuery, " +
    18. $"series_id: {(ulong?)row["series_id"]}, " +
    19. $"season_id: {(ulong?)row["season_id"]}, " +
    20. $"episodes_count: {(ulong)row["episodes_count"]}");
    21. }
    22. }
    23. }