2022-09-20 10:23:39 +00:00
|
|
|
using System;
|
2023-12-14 08:58:18 +00:00
|
|
|
using TDengine.Driver;
|
|
|
|
|
using TDengine.Driver.Client;
|
2022-09-20 10:23:39 +00:00
|
|
|
|
|
|
|
|
namespace Examples
|
|
|
|
|
{
|
|
|
|
|
public class WSConnExample
|
|
|
|
|
{
|
2024-08-02 06:28:24 +00:00
|
|
|
// ANCHOR: main
|
2023-12-14 08:58:18 +00:00
|
|
|
static void Main(string[] args)
|
2022-09-20 10:23:39 +00:00
|
|
|
{
|
2024-08-02 09:12:14 +00:00
|
|
|
var connectionString =
|
|
|
|
|
"protocol=WebSocket;host=localhost;port=6041;useSSL=false;username=root;password=taosdata";
|
2024-08-02 06:28:24 +00:00
|
|
|
try
|
2022-09-20 10:23:39 +00:00
|
|
|
{
|
2024-08-02 06:28:24 +00:00
|
|
|
// Connect to TDengine server using WebSocket
|
2024-08-02 09:12:14 +00:00
|
|
|
var builder = new ConnectionStringBuilder(connectionString);
|
2024-08-02 06:28:24 +00:00
|
|
|
// Open connection with using block, it will close the connection automatically
|
|
|
|
|
using (var client = DbDriver.Open(builder))
|
|
|
|
|
{
|
2024-08-02 09:12:14 +00:00
|
|
|
Console.WriteLine("Connected to " + connectionString + " successfully.");
|
2024-08-02 06:28:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (TDengineError e)
|
|
|
|
|
{
|
|
|
|
|
// handle TDengine error
|
2024-08-02 09:12:14 +00:00
|
|
|
Console.WriteLine("Failed to connect to " + connectionString + "; ErrCode:" + e.Code +
|
|
|
|
|
"; ErrMessage: " + e.Error);
|
2024-08-02 06:28:24 +00:00
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
// handle other exceptions
|
2024-08-02 09:12:14 +00:00
|
|
|
Console.WriteLine("Failed to connect to " + connectionString + "; Err:" + e.Message);
|
2024-08-02 06:28:24 +00:00
|
|
|
throw;
|
2022-09-20 10:23:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-02 06:28:24 +00:00
|
|
|
// ANCHOR_END: main
|
2022-09-20 10:23:39 +00:00
|
|
|
}
|
2024-08-02 06:28:24 +00:00
|
|
|
}
|