Skip to content

Commit 007a40a

Browse files
Update README.md (#192)
1 parent 1af5757 commit 007a40a

File tree

1 file changed

+67
-35
lines changed

1 file changed

+67
-35
lines changed

README.md

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
[![Nuget](https://img.shields.io/nuget/v/Ydb.Sdk)](https://www.nuget.org/packages/Ydb.Sdk/)
22

33
# YDB .NET SDK
4-
YDB client libraries for .NET.
4+
Provides an ADO.NET standard implementation for working with YDB, as well as native clients for lightweight interaction with YDB.
55

66
## Prerequisites
7-
.NET 6 or .NET 7
7+
.NET 6 or above
8+
9+
## Features
10+
11+
- **ADO.NET**: Full support for standard ADO.NET interfaces including DbConnection, DbCommand, DbDataReader, and more. This allows you to use familiar methods and patterns for database operations while leveraging the power and flexibility of YDB.
12+
- **QueryClient**: A lightweight, high-performance native client for direct interaction with YDB tables.
813

914
## Versioning
1015

@@ -18,7 +23,48 @@ Major version zero (`0.y.z`) is considered prerelease and **do not guarantee any
1823
dotnet add package Ydb.Sdk
1924
```
2025

21-
## Usage
26+
## Usage ADO.NET
27+
28+
Example of using ADO.NET to execute a SQL query against YDB:
29+
30+
```c#
31+
var ydbConnectionBuilder = new YdbConnectionStringBuilder
32+
{
33+
Host = "server",
34+
Port = 2135,
35+
Database = "/my-ydb",
36+
UseTls = true,
37+
CredentialsProvider = credentialsProvider // Credentials provider, see "Credentials" section
38+
};
39+
40+
await using var connection = new YdbConnection(ydbConnectionBuilder);
41+
await connection.OpenAsync();
42+
43+
var ydbCommand = connection.CreateCommand();
44+
ydbCommand.CommandText = """
45+
SELECT series_id, season_id, episode_id, air_date, title
46+
FROM episodes
47+
WHERE series_id = @series_id AND season_id > @season_id
48+
ORDER BY series_id, season_id, episode_id
49+
LIMIT @limit_size;
50+
""";
51+
ydbCommand.Parameters.Add(new YdbParameter("$series_id", DbType.UInt64, 1U));
52+
ydbCommand.Parameters.Add(new YdbParameter("$season_id", DbType.UInt64, 1U));
53+
ydbCommand.Parameters.Add(new YdbParameter("$limit_size", DbType.UInt64, 3U));
54+
55+
var ydbDataReader = await ydbCommand.ExecuteReaderAsync();
56+
57+
_logger.LogInformation("Selected rows:");
58+
while (await ydbDataReader.ReadAsync())
59+
{
60+
_logger.LogInformation(
61+
"series_id: {series_id}, season_id: {season_id}, episode_id: {episode_id}, air_date: {air_date}, title: {title}",
62+
ydbDataReader.GetUint64(0), ydbDataReader.GetUint64(1), ydbDataReader.GetUint64(2),
63+
ydbDataReader.GetDateTime(3), ydbDataReader.GetString(4));
64+
}
65+
```
66+
67+
## Usage Native clients
2268

2369
To begin your work with YDB, create an instance of `Ydb.Sdk.Driver` class:
2470
```c#
@@ -36,6 +82,8 @@ using var driver = new Driver(
3682
await driver.Initialize(); // Make sure to await driver initialization
3783
```
3884

85+
After you have driver instance, you can use it to create clients for different YDB services.
86+
3987
### Credentials
4088
YDB SDK provides several standard ways for authentication:
4189
1) `Ydb.Sdk.Auth.AnonymousProvider`. Anonymous YDB access, mainly for tests purposes.
@@ -44,17 +92,15 @@ YDB SDK provides several standard ways for authentication:
4492

4593
For Yandex.Cloud specific authentication methods, consider using **[ydb-dotnet-yc](https://github.com/ydb-platform/ydb-dotnet-yc)**.
4694

47-
### TableClient
48-
After you have driver instance, you can use it to create clients for different YDB services. The straightforward example of querying data may look similar to the following example:
95+
### QueryClient
96+
97+
Example of using a query client to execute a simple query:
4998

5099
```c#
51-
// Create Ydb.Sdk.Table.TableClient using Driver instance.
52-
using var tableClient = new TableClient(driver, new TableClientConfig());
100+
// Create QueryClient using Driver instance.
101+
using var queryClient = new QueryService(driver);
53102

54-
// Execute operation on arbitrary session with default retry policy
55-
var response = await tableClient.SessionExec(async session =>
56-
{
57-
var query = @"
103+
var row = await queryClient.ReadRow(@"
58104
DECLARE $id AS Uint64;
59105
60106
SELECT
@@ -63,31 +109,17 @@ var response = await tableClient.SessionExec(async session =>
63109
release_date
64110
FROM series
65111
WHERE series_id = $id;
66-
";
67-
68-
return await session.ExecuteDataQuery(
69-
query: query,
70-
parameters: new Dictionary<string, YdbValue>
71-
{
72-
{ "$id", YdbValue.MakeUint64(id) }
73-
},
74-
// Begin serializable transaction and commit automatically after query execution
75-
txControl: TxControl.BeginSerializableRW().Commit(),
76-
);
77-
});
78-
79-
response.Status.EnsureSuccess();
80-
81-
var queryResponse = (ExecuteDataQueryResponse)response;
82-
var resultSet = queryResponse.Result.ResultSets[0];
112+
",
113+
new Dictionary<string, YdbValue>
114+
{
115+
{ "$id", YdbValue.MakeUint64(id) }
116+
}
117+
);
83118

84-
foreach (var row in resultSet.Rows)
85-
{
86-
Console.WriteLine($"> Series, " +
87-
$"series_id: {(ulong?)row["series_id"]}, " +
88-
$"title: {(string?)row["title"]}, " +
89-
$"release_date: {(DateTime?)row["release_date"]}");
90-
}
119+
Console.WriteLine($"> Series, " +
120+
$"series_id: {(ulong?)row["series_id"]}, " +
121+
$"title: {(string?)row["title"]}, " +
122+
$"release_date: {(DateTime?)row["release_date"]}");
91123
```
92124

93125
## Examples

0 commit comments

Comments
 (0)