Skip to content

Commit 74dadad

Browse files
authored
Merge pull request #4213 from ClickHouse/csharp
add sharp driver
2 parents 576ba1a + fd21d19 commit 74dadad

File tree

7 files changed

+227
-3
lines changed

7 files changed

+227
-3
lines changed

docs/integrations/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ We are actively compiling this list of ClickHouse integrations below, so it's no
211211
|Azure Synapse|<Image img={azure_synapse_logo} size="logo" alt="Azure Synapse logo"/>|Data ingestion|A cloud-based analytics service for big data and data warehousing.|[Documentation](/integrations/azure-synapse)|
212212
|Azure Data Factory|<Image img={azure_data_factory_logo} size="logo" alt="Azure Data Factory logo"/>|Data ingestion|A cloud-based data integration service that enables you to create, schedule, and orchestrate data workflows at scale.|[Documentation](/integrations/azure-data-factory)|
213213
|C++|<Image img={logo_cpp} alt="Cpp logo" size="logo"/>|Language client|C++ client for ClickHouse|[GitHub](https://github.com/ClickHouse/clickhouse-cpp)|
214-
|C#|<Csharpsvg className="image" alt="Csharp logo" style={{width: '3rem'}}/>|Language client|ADO.NET client implementation for ClickHouse|[GitHub](https://github.com/ClickHouse/clickhouse-cs)|
214+
|C#|<Csharpsvg className="image" alt="Csharp logo" style={{width: '3rem'}}/>|Language client|ADO.NET client implementation for ClickHouse|[Documentation](/integrations/csharp)|
215215
|Cassandra|<Image img={cassandra} alt="Cassandra logo" size="logo"/>|Data ingestion|Allows ClickHouse to use [Cassandra](https://cassandra.apache.org/) as a dictionary source.|[Documentation](/sql-reference/dictionaries/index.md#cassandra)|
216216
|CHDB|<Chdbsvg alt="CHDB logo" style={{width: '3rem' }}/>|AI/ML|An embedded OLAP SQL Engine|[GitHub](https://github.com/chdb-io/chdb#/),<br/>[Documentation](https://doc.chdb.io/)|
217217
|ClickHouse Client|<Clickhousesvg alt="ClickHouse logo" style={{width: '3rem' }}/>|SQL client|ClickHouse Client is the native command-line client for ClickHouse.|[Documentation](/interfaces/cli.md)|
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
---
2+
sidebar_label: 'C#'
3+
sidebar_position: 6
4+
keywords: ['clickhouse', 'cs', 'c#', '.net', 'dotnet', 'csharp', 'client', 'driver', 'connect', 'integrate']
5+
slug: /integrations/csharp
6+
description: 'The official C# client for connecting to ClickHouse.'
7+
title: 'ClickHouse C# Driver'
8+
---
9+
10+
# ClickHouse C# Client
11+
12+
The official C# client for connecting to ClickHouse.
13+
The client source code is available in the [GitHub repository](https://github.com/ClickHouse/clickhouse-cs).
14+
Originally developed by [Oleg V. Kozlyuk](https://github.com/DarkWanderer).
15+
16+
## Supported .NET Versions {#supported-net-versions}
17+
18+
`ClickHouse.Driver` supports the following .NET versions:
19+
* .NET Framework 4.6.2
20+
* .NET Framework 4.8
21+
* .NET Standard 2.1
22+
* .NET 6.0
23+
* .NET 8.0
24+
* .NET 9.0
25+
26+
## Installation {#installation}
27+
28+
Install the package from NuGet:
29+
30+
```bash
31+
dotnet add package ClickHouse.Driver
32+
```
33+
34+
Or using the NuGet Package Manager:
35+
36+
```bash
37+
Install-Package ClickHouse.Driver
38+
```
39+
40+
## Usage {#usage}
41+
42+
### Creating a Connection {#creating-a-connection}
43+
44+
Create a connection using a connection string:
45+
46+
```csharp
47+
using ClickHouse.Driver.ADO;
48+
49+
var connectionString = "Host=localhost;Protocol=http;Database=default;Username=default;Password=";
50+
51+
using (var connection = new ClickHouseConnection(connectionString))
52+
{
53+
connection.Open();
54+
}
55+
```
56+
57+
### Creating a Table {#creating-a-table}
58+
59+
Create a table using standard SQL syntax:
60+
61+
```csharp
62+
using ClickHouse.Driver.ADO;
63+
64+
using (var connection = new ClickHouseConnection(connectionString))
65+
{
66+
connection.Open();
67+
68+
using (var command = connection.CreateCommand())
69+
{
70+
command.CommandText = "CREATE TABLE IF NOT EXISTS default.my_table (id Int64, name String) ENGINE = Memory";
71+
command.ExecuteNonQuery();
72+
}
73+
}
74+
```
75+
76+
### Inserting Data {#inserting-data}
77+
78+
Insert data using parameterized queries:
79+
80+
```csharp
81+
using ClickHouse.Driver.ADO;
82+
83+
using (var connection = new ClickHouseConnection(connectionString))
84+
{
85+
connection.Open();
86+
87+
using (var command = connection.CreateCommand())
88+
{
89+
command.AddParameter("id", "Int64", 1);
90+
command.AddParameter("name", "String", "test");
91+
command.CommandText = "INSERT INTO default.my_table (id, name) VALUES ({id:Int64}, {name:String})";
92+
command.ExecuteNonQuery();
93+
}
94+
}
95+
```
96+
97+
### Bulk Insert {#bulk-insert}
98+
99+
```csharp
100+
using ClickHouse.Driver.ADO;
101+
using ClickHouse.Driver.Copy;
102+
103+
using (var connection = new ClickHouseConnection(connectionString))
104+
{
105+
connection.Open();
106+
107+
using var bulkInsert = new ClickHouseBulkCopy(connection)
108+
{
109+
DestinationTableName = "default.my_table",
110+
MaxDegreeOfParallelism = 2,
111+
BatchSize = 100
112+
};
113+
114+
var values = Enumerable.Range(0, 100).Select(i => new object[] { (long)i, "value" + i.ToString() });
115+
await bulkInsert.WriteToServerAsync(values);
116+
Console.WriteLine($"Rows written: {bulkInsert.RowsWritten}");
117+
}
118+
```
119+
120+
### Performing SELECT Queries {#performing-select-queries}
121+
122+
Execute SELECT queries and process results:
123+
124+
```csharp
125+
using ClickHouse.Client.ADO;
126+
using System.Data;
127+
128+
using (var connection = new ClickHouseConnection(connectionString))
129+
{
130+
connection.Open();
131+
132+
using (var command = connection.CreateCommand())
133+
{
134+
command.AddParameter("id", "Int64", 10);
135+
command.CommandText = "SELECT * FROM default.my_table WHERE id < {id:Int64}";
136+
using var reader = command.ExecuteReader();
137+
while (reader.Read())
138+
{
139+
Console.WriteLine($"select: Id: {reader.GetInt64(0)}, Name: {reader.GetString(1)}");
140+
}
141+
}
142+
}
143+
```
144+
### Raw streaming {#raw-streaming}
145+
```csharp
146+
using var command = connection.CreateCommand();
147+
command.Text = "SELECT * FROM default.my_table LIMIT 100 FORMAT JSONEachRow";
148+
using var result = await command.ExecuteRawResultAsync(CancellationToken.None);
149+
using var stream = await result.ReadAsStreamAsync();
150+
using var reader = new StreamReader(stream);
151+
var json = reader.ReadToEnd();
152+
```
153+
154+
## Supported Data Types {#supported-data-types}
155+
156+
`ClickHouse.Driver` supports the following ClickHouse data types:
157+
**Boolean Type**
158+
* `Bool` (bool)
159+
160+
**Numeric Types**:
161+
* `Int8` (sbyte)
162+
* `Int16` (short)
163+
* `Int32` (int)
164+
* `Int64` (long)
165+
* `Int128` (BigInteger)
166+
* `Int256` (BigInteger)
167+
* `UInt8` (byte)
168+
* `UInt16` (ushort)
169+
* `UInt32` (uint)
170+
* `UInt64` (ulong)
171+
* `UInt128` (BigInteger)
172+
* `UInt256` (BigInteger)
173+
* `Float32` (float)
174+
* `Float64` (double)
175+
* `Decimal` (decimal)
176+
* `Decimal32` (decimal)
177+
* `Decimal64` (decimal)
178+
* `Decimal256` (BigDecimal)
179+
180+
**String Types**
181+
* `String` (string)
182+
* `FixedString` (string)
183+
184+
**Date and Time Types**
185+
* `Date` (DateTime)
186+
* `Date32` (DateTime)
187+
* `DateTime` (DateTime)
188+
* `DateTime32` (DateTime)
189+
* `DateTime64` (DateTime)
190+
191+
**Network Types**
192+
* `IPv4` (IPAddress)
193+
* `IPv6` (IPAddress)
194+
195+
**Geo Types**
196+
* `Point` (Tuple)
197+
* `Ring` (Array of Points)
198+
* `Polygon` (Array of Rings)
199+
200+
**Complex Types**
201+
* `Array` (Array of any type)
202+
* `Tuple` (Tuple of any types)
203+
* `Nullable` (Nullable version of any type)
204+
205+
### DateTime handling {#datetime-handling}
206+
`ClickHouse.Driver` tries to correctly handle timezones and `DateTime.Kind` property. Specifically:
207+
208+
`DateTime` values are returned as UTC. User can then convert them themselves or use `ToLocalTime()` method on `DateTime` instance.
209+
When inserting, `DateTime` values are handled in following way:
210+
- UTC `DateTime` are inserted as is, because ClickHouse stores them in UTC internally
211+
- Local `DateTime` are converted to UTC according to user's local timezone settings
212+
- Unspecified `DateTime` are considered to be in target column's timezone, and hence are converted to UTC according to that timezone

docs/integrations/language-clients/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ that ClickHouse offers.
1111
| Page | Description |
1212
|-------------------------------------------------------------------------|----------------------------------------------------------------------------------|
1313
| [C++](/interfaces/cpp) | C++ Client Library and userver Asynchronous Framework |
14+
| [C#](/integrations/csharp) | Learn how to connect your C# projects to ClickHouse. |
1415
| [Go](/integrations/go) | Learn how to connect your Go projects to ClickHouse. |
1516
| [JavaScript](/integrations/javascript) | Learn how to connect your JS projects to ClickHouse with the official JS client. |
1617
| [Java](/integrations/java) | Learn more about several integrations for Java and ClickHouse. |

docs/integrations/language-clients/rust.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
sidebar_label: 'Rust'
3-
sidebar_position: 4
3+
sidebar_position: 5
44
keywords: ['clickhouse', 'rs', 'rust', 'cargo', 'crate', 'http', 'client', 'connect', 'integrate']
55
slug: /integrations/rust
66
description: 'The official Rust client for connecting to ClickHouse.'

docusaurus.config.en.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ const config = {
283283
prism: {
284284
theme: prismLight,
285285
darkTheme: prismDark,
286-
additionalLanguages: ["java", "cpp", "rust", "python", "javascript", "yaml", "bash", "docker"],
286+
additionalLanguages: ["java", "cpp", "rust", "python", "javascript", "yaml", "bash", "docker", "csharp"],
287287
magicComments: [
288288
// Remember to extend the default highlight class name as well!
289289
{

scripts/aspell-dict-file.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,16 @@ microsoft
10641064
clickstack
10651065
--docs/use-cases/AI_ML/MCP/librechat.md--
10661066
librechat
1067+
--docs/integrations/language-clients/csharp.md--
1068+
BigDecimal
1069+
BigInteger
1070+
IPAddress
1071+
Kozlyuk
1072+
NuGet
1073+
Oleg
1074+
sbyte
1075+
ulong
1076+
ushort
10671077
--docs/whats-new/roadmap.md--
10681078
roadmaps
10691079
--docs/integrations/language-clients/python/index.md--

sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ const sidebars = {
684684
},
685685
"integrations/language-clients/python/index",
686686
"integrations/language-clients/rust",
687+
"integrations/language-clients/csharp",
687688
{
688689
type: "category",
689690
label: "Third-party Clients",

0 commit comments

Comments
 (0)