From c907546946a19c44b68c821106364727b7423ffc Mon Sep 17 00:00:00 2001 From: xiaolei li <85657333+xleili@users.noreply.github.com> Date: Wed, 13 Apr 2022 20:20:17 +0800 Subject: [PATCH] docs:docs-cn-example csharp fix example code && fix reference ident (#11457) * docs:docs-cn-example csharp fix example code && fix reference ident * use 'dotnet format' to correct coding style Co-authored-by: Shuduo Sang --- docs-cn/14-reference/03-connector/csharp.mdx | 3 +- docs-examples/csharp/AsyncQueryExample.cs | 3 +- docs-examples/csharp/ConnectExample.cs | 8 +- docs-examples/csharp/InfluxDBLineExample.cs | 6 +- docs-examples/csharp/OptsJsonExample.cs | 2 +- docs-examples/csharp/QueryExample.cs | 78 ++++++++++++++------ docs-examples/csharp/SQLInsertExample.cs | 10 ++- docs-examples/csharp/StmtInsertExample.cs | 15 ++-- 8 files changed, 85 insertions(+), 40 deletions(-) diff --git a/docs-cn/14-reference/03-connector/csharp.mdx b/docs-cn/14-reference/03-connector/csharp.mdx index af9b999950..11b3b7c3d3 100644 --- a/docs-cn/14-reference/03-connector/csharp.mdx +++ b/docs-cn/14-reference/03-connector/csharp.mdx @@ -8,7 +8,7 @@ title: C# Connector - C# 连接器支持的系统有:Linux 64/Windows x64/Windows x86 -- C# 连接器现在也支持从 [Nuget 下载引用](https://www.nuget.org/packages/TDengine.Connector/) +- C# 连接器支持从 [Nuget 下载引用](https://www.nuget.org/packages/TDengine.Connector/) - 在 Windows 系统上,C# 应用程序可以使用 TDengine 的原生 C 接口来执行所有数据库操作,后续版本将提供 ORM(Dapper)框架驱动。 @@ -26,6 +26,7 @@ title: C# Connector - [C# example source code on GitHub](https://github.com/taosdata/TDengine/tree/develop/examples/C%23) :::note + TDengineTest.cs C# 示例源程序,包含了数据库连接参数,以及如何执行数据插入、查询等操作。 ::: diff --git a/docs-examples/csharp/AsyncQueryExample.cs b/docs-examples/csharp/AsyncQueryExample.cs index 3391a74f5b..fe30d21efe 100644 --- a/docs-examples/csharp/AsyncQueryExample.cs +++ b/docs-examples/csharp/AsyncQueryExample.cs @@ -5,7 +5,8 @@ namespace TDengineExample { public class AsyncQueryExample { - static void Main() { + static void Main() + { IntPtr conn = GetConnection(); QueryAsyncCallback queryAsyncCallback = new QueryAsyncCallback(QueryCallback); TDengine.QueryAsync(conn, "select * from meters", queryAsyncCallback, IntPtr.Zero); diff --git a/docs-examples/csharp/ConnectExample.cs b/docs-examples/csharp/ConnectExample.cs index dafc02a7aa..f3548ee65d 100644 --- a/docs-examples/csharp/ConnectExample.cs +++ b/docs-examples/csharp/ConnectExample.cs @@ -6,20 +6,20 @@ namespace TDengineExample internal class ConnectExample { static void Main(String[] args) - { + { string host = "localhost"; short port = 6030; string username = "root"; string password = "taosdata"; string dbname = ""; - + var conn = TDengine.Connect(host, username, password, dbname, port); if (conn == IntPtr.Zero) { Console.WriteLine("Connect to TDengine failed"); } - else - { + else + { Console.WriteLine("Connect to TDengine success"); } TDengine.Close(conn); diff --git a/docs-examples/csharp/InfluxDBLineExample.cs b/docs-examples/csharp/InfluxDBLineExample.cs index 0c5fbef723..7aad088252 100644 --- a/docs-examples/csharp/InfluxDBLineExample.cs +++ b/docs-examples/csharp/InfluxDBLineExample.cs @@ -4,7 +4,8 @@ namespace TDengineExample { internal class InfluxDBLineExample { - static void Main() { + static void Main() + { IntPtr conn = GetConnection(); PrepareDatabase(conn); string[] lines = { @@ -52,7 +53,8 @@ namespace TDengineExample static void PrepareDatabase(IntPtr conn) { IntPtr res = TDengine.Query(conn, "CREATE DATABASE test"); - if (TDengine.ErrorNo(res) != 0) { + if (TDengine.ErrorNo(res) != 0) + { Console.WriteLine("failed to create database, reason: " + TDengine.Error(res)); ExitProgram(conn, 1); } diff --git a/docs-examples/csharp/OptsJsonExample.cs b/docs-examples/csharp/OptsJsonExample.cs index 5e19102139..d774a325af 100644 --- a/docs-examples/csharp/OptsJsonExample.cs +++ b/docs-examples/csharp/OptsJsonExample.cs @@ -13,7 +13,7 @@ namespace TDengineExample "{\"metric\": \"meters.current\", \"timestamp\": 1648432611250, \"value\": 12.6, \"tags\": {\"location\": \"Beijing.Chaoyang\", \"groupid\": 2}}," + " {\"metric\": \"meters.voltage\", \"timestamp\": 1648432611250, \"value\": 221, \"tags\": {\"location\": \"Beijing.Haidian\", \"groupid\": 1}}]" }; - + IntPtr res = TDengine.SchemalessInsert(conn, lines, 1, (int)TDengineSchemalessProtocol.TSDB_SML_JSON_PROTOCOL, (int)TDengineSchemalessPrecision.TSDB_SML_TIMESTAMP_NOT_CONFIGURED); if (TDengine.ErrorNo(res) != 0) { diff --git a/docs-examples/csharp/QueryExample.cs b/docs-examples/csharp/QueryExample.cs index 62fb84673f..f00e391100 100644 --- a/docs-examples/csharp/QueryExample.cs +++ b/docs-examples/csharp/QueryExample.cs @@ -5,13 +5,14 @@ namespace TDengineExample { internal class QueryExample { - static void Main() { + static void Main() + { IntPtr conn = GetConnection(); // run query - IntPtr res = TDengine.Query(conn, "SELECT * FROM meters LIMIT 2"); + IntPtr res = TDengine.Query(conn, "SELECT * FROM test.meters LIMIT 2"); if (TDengine.ErrorNo(res) != 0) { - Console.WriteLine("Failed to query since: " + TDengine.Error(res)); + Console.WriteLine("Failed to query since: " + TDengine.Error(res)); TDengine.Close(conn); TDengine.Cleanup(); return; @@ -19,7 +20,7 @@ namespace TDengineExample // get filed count int fieldCount = TDengine.FieldCount(res); - Console.WriteLine("fieldCount=" + fieldCount); + Console.WriteLine("fieldCount=" + fieldCount); // print column names List metas = TDengine.FetchFields(res); @@ -31,11 +32,22 @@ namespace TDengineExample // print values IntPtr row; - while ((row = TDengine.FetchRows(res)) != IntPtr.Zero) { - for (int i = 0; i < fieldCount; i++) { - var meta = metas[i]; - int offset = IntPtr.Size * i; - IntPtr data = Marshal.ReadIntPtr(row, offset); + while ((row = TDengine.FetchRows(res)) != IntPtr.Zero) + { + List metaList = TDengine.FetchFields(res); + int numOfFiled = TDengine.FieldCount(res); + + List dataRaw = new List(); + + IntPtr colLengthPrt = TDengine.FetchLengths(res); + int[] colLengthArr = new int[numOfFiled]; + Marshal.Copy(colLengthPrt, colLengthArr, 0, numOfFiled); + + for (int i = 0; i < numOfFiled; i++) + { + TDengineMeta meta = metaList[i]; + IntPtr data = Marshal.ReadIntPtr(row, IntPtr.Size * i); + if (data == IntPtr.Zero) { Console.Write("NULL\t"); @@ -45,51 +57,75 @@ namespace TDengineExample { case TDengineDataType.TSDB_DATA_TYPE_BOOL: bool v1 = Marshal.ReadByte(data) == 0 ? false : true; - Console.Write(v1 + "\t"); + Console.Write(v1.ToString() + "\t"); break; case TDengineDataType.TSDB_DATA_TYPE_TINYINT: - byte v2 = Marshal.ReadByte(data); - Console.Write(v2 + "\t"); + sbyte v2 = (sbyte)Marshal.ReadByte(data); + Console.Write(v2.ToString() + "\t"); break; case TDengineDataType.TSDB_DATA_TYPE_SMALLINT: short v3 = Marshal.ReadInt16(data); - Console.Write(v3 + "\t"); + Console.Write(v3.ToString() + "\t"); break; case TDengineDataType.TSDB_DATA_TYPE_INT: int v4 = Marshal.ReadInt32(data); - Console.Write(v4 + "\t"); + Console.Write(v4.ToString() + "\t"); break; case TDengineDataType.TSDB_DATA_TYPE_BIGINT: long v5 = Marshal.ReadInt64(data); - Console.Write(v5 + "\t"); + Console.Write(v5.ToString() + "\t"); break; case TDengineDataType.TSDB_DATA_TYPE_FLOAT: float v6 = (float)Marshal.PtrToStructure(data, typeof(float)); - Console.Write(v6 + "\t"); + Console.Write(v6.ToString() + "\t"); break; case TDengineDataType.TSDB_DATA_TYPE_DOUBLE: double v7 = (double)Marshal.PtrToStructure(data, typeof(double)); - Console.WriteLine(v7 + "\t"); + Console.Write(v7.ToString() + "\t"); break; case TDengineDataType.TSDB_DATA_TYPE_BINARY: - string v8 = Marshal.PtrToStringAnsi(data); + string v8 = Marshal.PtrToStringUTF8(data, colLengthArr[i]); Console.Write(v8 + "\t"); break; case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP: long v9 = Marshal.ReadInt64(data); - Console.Write(v9 + "\t"); + Console.Write(v9.ToString() + "\t"); break; case TDengineDataType.TSDB_DATA_TYPE_NCHAR: - string v10 = Marshal.PtrToStringAnsi(data); + string v10 = Marshal.PtrToStringUTF8(data, colLengthArr[i]); Console.Write(v10 + "\t"); break; + case TDengineDataType.TSDB_DATA_TYPE_UTINYINT: + byte v12 = Marshal.ReadByte(data); + Console.Write(v12.ToString() + "\t"); + break; + case TDengineDataType.TSDB_DATA_TYPE_USMALLINT: + ushort v13 = (ushort)Marshal.ReadInt16(data); + Console.Write(v13.ToString() + "\t"); + break; + case TDengineDataType.TSDB_DATA_TYPE_UINT: + uint v14 = (uint)Marshal.ReadInt32(data); + Console.Write(v14.ToString() + "\t"); + break; + case TDengineDataType.TSDB_DATA_TYPE_UBIGINT: + ulong v15 = (ulong)Marshal.ReadInt64(data); + Console.Write(v15.ToString() + "\t"); + break; + case TDengineDataType.TSDB_DATA_TYPE_JSONTAG: + string v16 = Marshal.PtrToStringUTF8(data, colLengthArr[i]); + Console.Write(v16 + "\t"); + break; + default: + Console.Write("nonsupport data type value"); + break; } + } Console.WriteLine(); } if (TDengine.ErrorNo(res) != 0) { - Console.WriteLine($"Query is not complete, Error { TDengine.ErrorNo(res)} {TDengine.Error(res)}"); + Console.WriteLine($"Query is not complete, Error {TDengine.ErrorNo(res)} {TDengine.Error(res)}"); } // exit TDengine.FreeResult(res); diff --git a/docs-examples/csharp/SQLInsertExample.cs b/docs-examples/csharp/SQLInsertExample.cs index 3b81a0d0cc..fa2e2a50da 100644 --- a/docs-examples/csharp/SQLInsertExample.cs +++ b/docs-examples/csharp/SQLInsertExample.cs @@ -5,7 +5,7 @@ namespace TDengineExample { internal class SQLInsertExample { - + static void Main() { IntPtr conn = GetConnection(); @@ -26,7 +26,8 @@ namespace TDengineExample ExitProgram(conn, 0); } - static IntPtr GetConnection() { + static IntPtr GetConnection() + { string host = "localhost"; short port = 6030; string username = "root"; @@ -45,10 +46,11 @@ namespace TDengineExample return conn; } - static void CheckRes(IntPtr conn, IntPtr res, String errorMsg) { + static void CheckRes(IntPtr conn, IntPtr res, String errorMsg) + { if (TDengine.ErrorNo(res) != 0) { - Console.Write(errorMsg + " since: " + TDengine.Error(res)); + Console.Write(errorMsg + " since: " + TDengine.Error(res)); ExitProgram(conn, 1); } } diff --git a/docs-examples/csharp/StmtInsertExample.cs b/docs-examples/csharp/StmtInsertExample.cs index c7e5cdfbd7..d6e00dd4ac 100644 --- a/docs-examples/csharp/StmtInsertExample.cs +++ b/docs-examples/csharp/StmtInsertExample.cs @@ -12,7 +12,8 @@ namespace TDengineExample PrepareSTable(); // 1. init and prepare stmt = TDengine.StmtInit(conn); - if (stmt == IntPtr.Zero) { + if (stmt == IntPtr.Zero) + { Console.WriteLine("failed to init stmt, " + TDengine.Error(stmt)); ExitProgram(); } @@ -20,7 +21,7 @@ namespace TDengineExample CheckStmtRes(res, "failed to prepare stmt"); // 2. bind table name and tags - TAOS_BIND[] tags = new TAOS_BIND[2] {TaosBind.BindBinary("Beijing.Chaoyang"), TaosBind.BindInt(2) }; + TAOS_BIND[] tags = new TAOS_BIND[2] { TaosBind.BindBinary("Beijing.Chaoyang"), TaosBind.BindInt(2) }; res = TDengine.StmtSetTbnameTags(stmt, "d1001", tags); CheckStmtRes(res, "failed to bind table name and tags"); @@ -37,7 +38,7 @@ namespace TDengineExample // 4. add batch res = TDengine.StmtAddBatch(stmt); CheckStmtRes(res, "failed to add batch"); - + // 5. execute res = TDengine.StmtExecute(stmt); CheckStmtRes(res, "faild to execute"); @@ -69,9 +70,10 @@ namespace TDengineExample return conn; } - - static void PrepareSTable() { + + static void PrepareSTable() + { IntPtr res = TDengine.Query(conn, "CREATE DATABASE power"); CheckResPtr(res, "failed to create database"); res = TDengine.Query(conn, "USE power"); @@ -86,7 +88,8 @@ namespace TDengineExample { Console.WriteLine(errorMsg + ", " + TDengine.StmtErrorStr(stmt)); int code = TDengine.StmtClose(stmt); - if (code != 0) { + if (code != 0) + { Console.WriteLine($"falied to close stmt, {code} reason: {TDengine.StmtErrorStr(stmt)} "); } ExitProgram(); -- GitLab