C# 毗连ClickHouse 数据库

[复制链接]
发表于 2025-12-2 01:13:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
在 C# 中毗连 ClickHouse 数据库,您可以利用 ClickHouse.Client 库。这个库提供了对 ClickHouse 数据库的高效访问。以下是详细的步调指南,资助您在 C# 项目中毗连和操纵 ClickHouse 数据库。
1. 安装 ClickHouse.Client 包
起首,您必要在您的项目中安装 ClickHouse.Client 包。您可以利用 NuGet 包管理器来完成此操纵。
利用 NuGet 包管理器控制台
  1. Install-Package ClickHouse.Client -Version 1.4.1
复制代码
利用 .NET CLI
  1. dotnet add package ClickHouse.Client --version 1.4.1
复制代码
2. 设置 ClickHouse 客户端
接下来,您必要设置 ClickHouse 客户端以毗连到您的 ClickHouse 实例。以下是一个根本的设置示例。
  1. using ClickHouse.Client;
  2. using ClickHouse.Client.ADO;
  3. using System;
  4. using System.Data;
  5. namespace ClickHouseExample
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             // 配置 ClickHouse 连接字符串
  12.             string connectionString = "Host=127.0.0.1;Port=9000;Username=default;Password=;Database=default";
  13.             using (var connection = new ClickHouseConnection(connectionString))
  14.             {
  15.                 try
  16.                 {
  17.                     connection.Open();
  18.                     Console.WriteLine("Connected to ClickHouse!");
  19.                     // 执行查询
  20.                     using (var command = new ClickHouseCommand("SELECT * FROM system.numbers LIMIT 10", connection))
  21.                     {
  22.                         using (var reader = command.ExecuteReader())
  23.                         {
  24.                             while (reader.Read())
  25.                             {
  26.                                 Console.WriteLine(reader[0]);
  27.                             }
  28.                         }
  29.                     }
  30.                 }
  31.                 catch (Exception ex)
  32.                 {
  33.                     Console.WriteLine($"Error connecting to ClickHouse: {ex.Message}");
  34.                 }
  35.             }
  36.         }
  37.     }
  38. }
复制代码
3. 创建表
如果您还没有创建表,可以利用 ClickHouse.Client 创建一个新的表。
  1. using ClickHouse.Client;
  2. using ClickHouse.Client.ADO;
  3. using System;
  4. namespace ClickHouseExample
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string connectionString = "Host=127.0.0.1;Port=9000;Username=default;Password=;Database=default";
  11.             using (var connection = new ClickHouseConnection(connectionString))
  12.             {
  13.                 try
  14.                 {
  15.                     connection.Open();
  16.                     Console.WriteLine("Connected to ClickHouse!");
  17.                     // 创建表
  18.                     string createTableQuery = @"
  19.                         CREATE TABLE IF NOT EXISTS my_table
  20.                         (
  21.                             id UInt32,
  22.                             name String,
  23.                             description String
  24.                         ) ENGINE = MergeTree() ORDER BY id;
  25.                     ";
  26.                     using (var command = new ClickHouseCommand(createTableQuery, connection))
  27.                     {
  28.                         command.ExecuteNonQuery();
  29.                         Console.WriteLine("Table created successfully.");
  30.                     }
  31.                 }
  32.                 catch (Exception ex)
  33.                 {
  34.                     Console.WriteLine($"Error connecting to ClickHouse: {ex.Message}");
  35.                 }
  36.             }
  37.         }
  38.     }
  39. }
复制代码
4. 插入数据
您可以利用 ClickHouse.Client 将数据插入到 ClickHouse 中。
  1. using ClickHouse.Client;
  2. using ClickHouse.Client.ADO;
  3. using System;
  4. namespace ClickHouseExample
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string connectionString = "Host=127.0.0.1;Port=9000;Username=default;Password=;Database=default";
  11.             using (var connection = new ClickHouseConnection(connectionString))
  12.             {
  13.                 try
  14.                 {
  15.                     connection.Open();
  16.                     Console.WriteLine("Connected to ClickHouse!");
  17.                     // 插入数据
  18.                     string insertQuery = @"
  19.                         INSERT INTO my_table (id, name, description) VALUES
  20.                         (1, 'Sample Document', 'This is a sample document.'),
  21.                         (2, 'Another Document', 'This is another sample document.');
  22.                     ";
  23.                     using (var command = new ClickHouseCommand(insertQuery, connection))
  24.                     {
  25.                         command.ExecuteNonQuery();
  26.                         Console.WriteLine("Data inserted successfully.");
  27.                     }
  28.                 }
  29.                 catch (Exception ex)
  30.                 {
  31.                     Console.WriteLine($"Error connecting to ClickHouse: {ex.Message}");
  32.                 }
  33.             }
  34.         }
  35.     }
  36. }
复制代码
5. 查询数据
您可以利用 ClickHouse.Client 实行查询以检索数据。
  1. using ClickHouse.Client;
  2. using ClickHouse.Client.ADO;
  3. using System;
  4. using System.Data;
  5. namespace ClickHouseExample
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string connectionString = "Host=127.0.0.1;Port=9000;Username=default;Password=;Database=default";
  12.             using (var connection = new ClickHouseConnection(connectionString))
  13.             {
  14.                 try
  15.                 {
  16.                     connection.Open();
  17.                     Console.WriteLine("Connected to ClickHouse!");
  18.                     // 查询数据
  19.                     string selectQuery = "SELECT * FROM my_table";
  20.                     using (var command = new ClickHouseCommand(selectQuery, connection))
  21.                     {
  22.                         using (var reader = command.ExecuteReader())
  23.                         {
  24.                             while (reader.Read())
  25.                             {
  26.                                 Console.WriteLine($"Id: {reader["id"]}, Name: {reader["name"]}, Description: {reader["description"]}");
  27.                             }
  28.                         }
  29.                     }
  30.                 }
  31.                 catch (Exception ex)
  32.                 {
  33.                     Console.WriteLine($"Error connecting to ClickHouse: {ex.Message}");
  34.                 }
  35.             }
  36.         }
  37.     }
  38. }
复制代码
6. 更新数据
ClickHouse 不直接支持 UPDATE 操纵,但您可以利用 ALTER TABLE ... UPDATE 语句来更新数据。不外,这种操纵相对复杂且性能较低,通常发起利用 INSERT 和 DELETE 组合来实现类似的结果。
  1. using ClickHouse.Client;
  2. using ClickHouse.Client.ADO;
  3. using System;
  4. namespace ClickHouseExample
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string connectionString = "Host=127.0.0.1;Port=9000;Username=default;Password=;Database=default";
  11.             using (var connection = new ClickHouseConnection(connectionString))
  12.             {
  13.                 try
  14.                 {
  15.                     connection.Open();
  16.                     Console.WriteLine("Connected to ClickHouse!");
  17.                     // 更新数据
  18.                     string updateQuery = @"
  19.                         ALTER TABLE my_table UPDATE name = 'Updated Document' WHERE id = 1;
  20.                     ";
  21.                     using (var command = new ClickHouseCommand(updateQuery, connection))
  22.                     {
  23.                         command.ExecuteNonQuery();
  24.                         Console.WriteLine("Data updated successfully.");
  25.                     }
  26.                 }
  27.                 catch (Exception ex)
  28.                 {
  29.                     Console.WriteLine($"Error connecting to ClickHouse: {ex.Message}");
  30.                 }
  31.             }
  32.         }
  33.     }
  34. }
复制代码
7. 删除数据
您可以利用 ClickHouse.Client 删除数据。
  1. using ClickHouse.Client;
  2. using ClickHouse.Client.ADO;
  3. using System;
  4. namespace ClickHouseExample
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string connectionString = "Host=127.0.0.1;Port=9000;Username=default;Password=;Database=default";
  11.             using (var connection = new ClickHouseConnection(connectionString))
  12.             {
  13.                 try
  14.                 {
  15.                     connection.Open();
  16.                     Console.WriteLine("Connected to ClickHouse!");
  17.                     // 删除数据
  18.                     string deleteQuery = @"
  19.                         ALTER TABLE my_table DELETE WHERE id = 2;
  20.                     ";
  21.                     using (var command = new ClickHouseCommand(deleteQuery, connection))
  22.                     {
  23.                         command.ExecuteNonQuery();
  24.                         Console.WriteLine("Data deleted successfully.");
  25.                     }
  26.                 }
  27.                 catch (Exception ex)
  28.                 {
  29.                     Console.WriteLine($"Error connecting to ClickHouse: {ex.Message}");
  30.                 }
  31.             }
  32.         }
  33.     }
  34. }
复制代码
总结
通过以上步调,您可以在 C# 项目中乐成毗连和操纵 ClickHouse 数据库。ClickHouse.Client 提供了丰富的 API 来实行各种操纵,如创建表、插入数据、查询数据、更新数据和删除数据。确保您的 ClickHouse 实例正在运行,而且客户端设置精确,以便顺遂举行这些操纵。如果遇到任何题目,可以参考 ClickHouse.Client 的官方文档或社区资源以获取更多资助。
参考资料:
•  ClickHouse 官方文档  https://clickhouse.com/docs/zh/getting-started/install
•  ClickHouse.Client https://github.com/yandex/ClickHouse.Client
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表