TDengine/docs/examples/csharp/wsConnect/Program.cs

40 lines
1.3 KiB
C#
Raw Normal View History

using System;
using TDengine.Driver;
using TDengine.Driver.Client;
namespace Examples
{
public class WSConnExample
{
2024-08-02 06:28:24 +00:00
// ANCHOR: main
static void Main(string[] args)
{
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
{
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;
}
}
2024-08-02 06:28:24 +00:00
// ANCHOR_END: main
}
2024-08-02 06:28:24 +00:00
}