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 UsageExample
{
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};" ;
2024-11-19 02:35:32 +00:00
// Connect to TDengine server using WebSocket
2024-11-20 03:30:25 +00:00
var builder = new ConnectionStringBuilder ( connectionString ) ;
2024-11-19 02:35:32 +00:00
try
2022-09-02 10:51:12 +00:00
{
2024-11-19 02:35:32 +00:00
// Open connection with using block, it will close the connection automatically
using ( var client = DbDriver . Open ( builder ) )
{
InsertData ( client ) ;
SelectData ( client ) ;
}
} catch ( TDengineError e )
{
// handle TDengine error
Console . WriteLine ( e . Message ) ;
throw ;
}
catch ( Exception e )
{
// handle other exceptions
Console . WriteLine ( e . Message ) ;
throw ;
}
2022-09-02 10:51:12 +00:00
}
2024-11-19 02:35:32 +00:00
public static void InsertData ( ITDengineClient client )
2022-09-02 10:51:12 +00:00
{
string createTable = "CREATE STABLE if not exists test.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)" ;
string insertData = "INSERT INTO " +
2024-11-19 02:35:32 +00:00
"test.d1001 USING test.meters1 TAGS('California.SanFrancisco', 1) VALUES ('2018-10-03 14:38:05.000', 10.30000, 219, 0.31000)" +
"test.d1002 USING test.meters1 TAGS('California.SanFrancisco', 2) VALUES ('2018-10-03 14:38:16.650', 10.30000, 218, 0.25000)" +
"test.d1003 USING test.meters1 TAGS('California.LosAngeles', 3) VALUES ('2018-10-03 14:38:05.500', 11.80000, 221, 0.28000)" +
"test.d1004 USING test.meters1 TAGS('California.LosAngeles', 4) VALUES ('2018-10-03 14:38:05.000', 10.80000, 223, 0.29000) " ;
2022-09-02 10:51:12 +00:00
2024-11-19 02:35:32 +00:00
// create stable under database named 'test'
var affected = client . Exec ( createTable ) ;
Console . WriteLine ( $"Create stable meters, affected rows: {affected}" ) ;
2022-09-02 10:51:12 +00:00
// insert data into the table created in previous step.
2024-11-19 02:35:32 +00:00
affected = client . Exec ( insertData ) ;
Console . WriteLine ( "insert " + affected + " rows to test.meters successfully." ) ;
2022-09-02 10:51:12 +00:00
}
2024-11-19 02:35:32 +00:00
public static void SelectData ( ITDengineClient client )
2022-09-02 10:51:12 +00:00
{
2024-11-19 02:35:32 +00:00
string selectTable = "select * from test.meters" ;
using ( var rows = client . Query ( selectTable ) )
2022-09-02 10:51:12 +00:00
{
2024-11-19 02:35:32 +00:00
while ( rows . Read ( ) )
{
var ts = ( DateTime ) rows . GetValue ( 0 ) ;
var current = ( float ) rows . GetValue ( 1 ) ;
var voltage = ( int ) rows . GetValue ( 2 ) ;
var phase = ( float ) rows . GetValue ( 3 ) ;
var location = Encoding . UTF8 . GetString ( ( byte [ ] ) rows . GetValue ( 4 ) ) ;
Console . WriteLine (
$"ts: {ts:yyyy-MM-dd HH:mm:ss.fff}, current: {current}, voltage: {voltage}, phase: {phase}, location: {location}" ) ;
}
2022-09-02 10:51:12 +00:00
}
}
}
}