|
| 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 |
0 commit comments