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

37 lines
1.3 KiB
C#
Raw Permalink Normal View History

using TDengine.Driver;
using TDengine.Driver.Client;
namespace TDengineExample
{
internal class ConnectExample
{
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 = "host=127.0.0.1;port=6030;username=root;password=taosdata";
2024-08-02 06:28:24 +00:00
try
{
// Connect to TDengine server using Native
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)
{
2024-08-02 06:28:24 +00:00
// 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
}