Skip to main content

Connect with C#

Create Project

dotnet new console -o example

Add C# TDengine Driver class lib

cd example
vim example.csproj

Add following ItemGroup and Task to your project file.

<ItemGroup>
<PackageReference Include="TDengine.Connector" Version="3.0.*" GeneratePathProperty="true" />
</ItemGroup>
<Target Name="copyDLLDependency" BeforeTargets="BeforeBuild">
<ItemGroup>
<DepDLLFiles Include="$(PkgTDengine_Connector)\runtimes\**\*.*" />
</ItemGroup>
<Copy SourceFiles="@(DepDLLFiles)" DestinationFolder="$(OutDir)" />
</Target>
dotnet add package TDengine.Connector

Config

Run this command in your terminal to save TDengine cloud token as variables:

export TDENGINE_CLOUD_DSN="<DSN>"
IMPORTANT

Replace <DSN> with real TDengine cloud DSN. To obtain the real value, please log in TDengine Cloud and click "Programming" on the left menu, then select "C#".

Connect

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="TDengine.Connector" Version="3.0.*" GeneratePathProperty="true" />
</ItemGroup>
<Target Name="copyDLLDependency" BeforeTargets="BeforeBuild">
<ItemGroup>
<DepDLLFiles Include="$(PkgTDengine_Connector)\runtimes\**\*.*" />
</ItemGroup>
<Copy SourceFiles="@(DepDLLFiles)" DestinationFolder="$(OutDir)" />
</Target>

</Project>

view source code

using System;
using TDengineWS.Impl;

namespace Cloud.Examples
{
public class ConnectExample
{
static void Main(string[] args)
{
string dsn = Environment.GetEnvironmentVariable("TDENGINE_CLOUD_DSN");
Connect(dsn);
}

public static void Connect(string dsn)
{
// get connect
IntPtr conn = LibTaosWS.WSConnectWithDSN(dsn);
if (conn == IntPtr.Zero)
{
throw new Exception($"get connection failed,reason:{LibTaosWS.WSErrorStr(conn)},code:{LibTaosWS.WSErrorNo(conn)}");
}
else
{
Console.WriteLine("Establish connect success.");
}

// do something ...

// close connect
LibTaosWS.WSClose(conn);

}
}
}

view source code

The client connection is then established. For how to write data and query data, please refer to Data In and Tools.

For more details about how to write or query data via REST API, please check REST API.