2022-09-05 09:50:06 +00:00
|
|
|
using System;
|
2024-11-19 02:35:32 +00:00
|
|
|
using System.Text;
|
|
|
|
|
using TDengine.Driver;
|
|
|
|
|
using TDengine.Driver.Client;
|
2022-09-02 10:51:12 +00:00
|
|
|
|
|
|
|
|
namespace Cloud.Examples
|
|
|
|
|
{
|
|
|
|
|
public class ConnectExample
|
|
|
|
|
{
|
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
2024-11-20 06:36:55 +00:00
|
|
|
var cloudEndPoint = Environment.GetEnvironmentVariable("TDENGINE_CLOUD_ENDPOINT");
|
|
|
|
|
var cloudToken = Environment.GetEnvironmentVariable("TDENGINE_CLOUD_TOKEN");
|
2024-11-20 03:30:25 +00:00
|
|
|
var connectionString = $"protocol=WebSocket;host={cloudEndPoint};port=443;useSSL=true;token={cloudToken};";
|
|
|
|
|
// Connect to TDengine server using WebSocket
|
|
|
|
|
var builder = new ConnectionStringBuilder(connectionString);
|
2024-11-19 02:35:32 +00:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Open connection with using block, it will close the connection automatically
|
|
|
|
|
using (var client = DbDriver.Open(builder))
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Connected to " + builder.ToString() + " successfully.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (TDengineError e)
|
|
|
|
|
{
|
|
|
|
|
// handle TDengine error
|
|
|
|
|
Console.WriteLine("Failed to connect to " + builder.ToString() + "; ErrCode:" + e.Code +
|
|
|
|
|
"; ErrMessage: " + e.Error);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
// handle other exceptions
|
|
|
|
|
Console.WriteLine("Failed to connect to " + builder.ToString() + "; Err:" + e.Message);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2022-09-02 10:51:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|