Closed windjmj closed 5 years ago
Well, i have the same questions, is it possible to call TDEngine via C++ lib from C# application ?@windjmj
We will provide the C# and library for windows soon. Please wait for a few weeks.
Restfull api is an alternative choice
yep,restful api indeed can meet your requirment, but cannt subscribe ,right?
If the TDengine can support C# for dotnet core platform, it will be better, after all, dotnet core is the future.
string database = "db_" + DateTime.Now.ToString("yyyyMMddHHmmss");
var builder = new TaosConnectionStringBuilder()
{
DataSource = "http://td.gitclub.cn/rest/sql",
DataBase = database,
// Token = "cm9vdDp0YW9zZGF0YQ=="
Username="root",
Password= "taosdata"
};
using (var connection = new TaosConnection(builder.ConnectionString))
{
connection.Open();
Console.WriteLine("create {0} {1}", database, connection.CreateCommand($"create database {database};").ExecuteNonQuery());
Console.WriteLine("create table t {0} {1}", database, connection.CreateCommand($"create table {database}.t (ts timestamp, cdata int);").ExecuteNonQuery());
Console.WriteLine("insert into t values {0} ", connection.CreateCommand($"insert into {database}.t values ('{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ms")}', 10);").ExecuteNonQuery());
Console.WriteLine("insert into t values {0} ", connection.CreateCommand($"insert into {database}.t values ('{DateTime.Now.AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss.ms")}', 20);").ExecuteNonQuery());
var cmd_select = connection.CreateCommand();
cmd_select.CommandText = $"select * from {database}.t";
var reader = cmd_select.ExecuteReader();
List<Dictionary<string, object>> valuePairs = new List<Dictionary<string, object>>();
while (reader.Read())
{
Dictionary<string, object> pairs = new Dictionary<string, object>();
for (int i = 0; i < reader.FieldCount; i++)
{
pairs.Add(reader.GetName(i), reader.GetValue(i));
}
valuePairs.Add(pairs);
}
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(valuePairs));
Console.WriteLine("DROP TABLE {0} {1}", database, connection.CreateCommand($"DROP TABLE {database}.t;").ExecuteNonQuery());
Console.WriteLine("DROP DATABASE {0} {1}", database, connection.CreateCommand($"DROP DATABASE {database}").ExecuteNonQuery());
Console.ReadKey();
connection.Close();
}
OK, restful api to ef.core is the first step, we still need more reliable and more efficient connection api from official.
///Specify the name of the database
string database = "db_" + DateTime.Now.ToString("yyyyMMddHHmmss");
var builder = new TaosConnectionStringBuilder()
{
DataSource = "http://td.gitclub.cn/rest/sql",
DataBase = database,
Username = "root",
Password = "taosdata"
};
//Example for ADO.Net
using (var connection = new TaosConnection(builder.ConnectionString))
{
connection.Open();
Console.WriteLine("create {0} {1}", database, connection.CreateCommand($"create database {database};").ExecuteNonQuery());
Console.WriteLine("create table t {0} {1}", database, connection.CreateCommand($"create table {database}.t (ts timestamp, cdata int);").ExecuteNonQuery());
Console.WriteLine("insert into t values {0} ", connection.CreateCommand($"insert into {database}.t values ('{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ms")}', 10);").ExecuteNonQuery());
Console.WriteLine("insert into t values {0} ", connection.CreateCommand($"insert into {database}.t values ('{DateTime.Now.AddMonths(1).ToString("yyyy-MM-dd HH:mm:ss.ms")}', 20);").ExecuteNonQuery());
Console.WriteLine("==================================================================");
var cmd_select = connection.CreateCommand();
cmd_select.CommandText = $"select * from {database}.t";
var reader = cmd_select.ExecuteReader();
ConsoleTableBuilder.From(reader.ToDataTable()).WithFormat(ConsoleTableBuilderFormat.MarkDown).ExportAndWriteLine();
Console.WriteLine("==================================================================");
Console.WriteLine("DROP TABLE {0} {1}", database, connection.CreateCommand($"DROP TABLE {database}.t;").ExecuteNonQuery());
Console.WriteLine("DROP DATABASE {0} {1}", database, connection.CreateCommand($"DROP DATABASE {database}").ExecuteNonQuery());
connection.Close();
}
//Example for Entity Framework Core
using (var context = new TaosContext(new DbContextOptionsBuilder()
.UseTaos(builder.ConnectionString).Options))
{
Console.WriteLine("EnsureCreated");
context.Database.EnsureCreated();
for (int i = 0; i < 10; i++)
{
var rd = new Random();
context.sensor.Add(new sensor() { ts = DateTime.Now.AddMilliseconds(i), degree = rd.NextDouble(), pm25 = rd.Next(0, 1000) });
}
Console.WriteLine("SaveChanges.....");
context.SaveChanges();
Console.WriteLine("==================================================================");
Console.WriteLine("Search pm25>0");
var f = from s in context.sensor where s.pm25 > 0 select s;
var ary = f.ToArray();
ConsoleTableBuilder.From(ary.ToList()).WithFormat(ConsoleTableBuilderFormat.MarkDown).ExportAndWriteLine();
Console.WriteLine("==================================================================");
context.Database.EnsureDeleted();
}
}
Thank you for the code.If the TDengine can support c# ,it will perfect.