csharp.mdx 2.9 KB
Newer Older
B
Bo Ding 已提交
1 2 3
---
sidebar_position: 7
sidebar_label: C#
D
dingbo 已提交
4
title: C# Connector
B
Bo Ding 已提交
5 6
---

D
dingbo 已提交
7
## 简介
B
Bo Ding 已提交
8 9 10

- C# 连接器支持的系统有:Linux 64/Windows x64/Windows x86

11
- C# 连接器支持从 [Nuget 下载引用](https://www.nuget.org/packages/TDengine.Connector/)
B
Bo Ding 已提交
12 13 14 15 16

- 在 Windows 系统上,C# 应用程序可以使用 TDengine 的原生 C 接口来执行所有数据库操作,后续版本将提供 ORM(Dapper)框架驱动。

## 安装准备

D
dingbo 已提交
17 18 19
- 应用驱动安装请参考[安装连接器驱动步骤](/reference/connector/#安装客户端驱动)。
- 接口文件 TDengineDrivercs.cs 和参考程序示例 TDengineTest.cs 均位于 Windows 客户端 install_directory/examples/C# 目录下。
- 安装 [.NET SDK](https://dotnet.microsoft.com/download)
B
Bo Ding 已提交
20 21 22 23 24 25

## 示例程序

示例程序源码位于

- {client_install_directory}/examples/C#
26
- [C# example source code on GitHub](https://github.com/taosdata/TDengine/tree/develop/examples/C%23)
B
Bo Ding 已提交
27

28
:::note
29

30 31 32
TDengineTest.cs C# 示例源程序,包含了数据库连接参数,以及如何执行数据插入、查询等操作。

:::
B
Bo Ding 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

## 安装验证

需要先安装 .NET SDK

```cmd
cd {client_install_directory}/examples/C#/C#Checker
//运行测试
dotnet run -- -h <FQDN>. // 此步骤会先build,然后再运行。
```

## C# 连接器的使用

在 Windows 系统上,C# 应用程序可以使用 TDengine 的 C# 连接器接口来执行所有数据库的操作。使用的具体步骤如下所示:

- 创建一个 C# project(需要 .NET SDK).

  ```cmd
  mkdir test
  cd test
  dotnet new console
  ```

- 通过 Nuget 引用 TDengineDriver 包

  ```cmd
  dotnet add package TDengine.Connector
  ```

- 在项目中需要用到 TDengineConnector 的地方引用 TDengineDriver namespace。

  ```cs
  using TDengineDriver;
  ```
67
- 用户可以参考[TDengineTest.cs](https://github.com/taosdata/TDengine/tree/develop/examples/C%23/TDengineTest)来定义数据库连接参数,以及如何执行数据插入、查询等操作。
B
Bo Ding 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

:::note

- TDengine V2.0.3.0 之后同时支持 32 位和 64 位 Windows 系统,所以 C# 项目在生成 .exe 文件时,“解决方案”/“项目”的“平台”请选择对应的 x86 或 x64。
- 此接口目前已经在 Visual Studio 2015/2017 中验证过,其它 Visual Studio 版本尚待验证。
- 此连接器需要用到 taos.dll 文件,所以在未安装客户端时需要在执行应用程序前,拷贝 Windows{client_install_directory}/driver 目录中的 taos.dll 文件到项目最后生成 .exe 可执行文件所在的文件夹。之后运行 exe 文件,即可访问 TDengine 数据库并做插入、查询等操作。

:::

## 第三方驱动

Maikebing.Data.Taos 是一个 TDengine 的 ADO.NET 提供器,支持 Linux,Windows。该开发包由热心贡献者`麦壳饼@@maikebing`提供,具体请参考:

- 接口下载:https://github.com/maikebing/Maikebing.EntityFrameworkCore.Taos
- 用法说明:https://www.taosdata.com/blog/2020/11/02/1901.html