QueryExample.cs 6.9 KB
Newer Older
B
Bo Ding 已提交
1 2 3 4 5 6 7
using TDengineDriver;
using System.Runtime.InteropServices;

namespace TDengineExample
{
    internal class QueryExample
    {
8 9
        static void Main()
        {
B
Bo Ding 已提交
10 11
            IntPtr conn = GetConnection();
            // run query
12
            IntPtr res = TDengine.Query(conn, "SELECT * FROM test.meters LIMIT 2");
B
Bo Ding 已提交
13 14
            if (TDengine.ErrorNo(res) != 0)
            {
15
                Console.WriteLine("Failed to query since: " + TDengine.Error(res));
B
Bo Ding 已提交
16 17 18 19 20 21 22
                TDengine.Close(conn);
                TDengine.Cleanup();
                return;
            }

            // get filed count
            int fieldCount = TDengine.FieldCount(res);
23
            Console.WriteLine("fieldCount=" + fieldCount);
B
Bo Ding 已提交
24 25 26 27 28 29 30 31 32 33 34

            // print column names
            List<TDengineMeta> metas = TDengine.FetchFields(res);
            for (int i = 0; i < metas.Count; i++)
            {
                Console.Write(metas[i].name + "\t");
            }
            Console.WriteLine();

            // print values
            IntPtr row;
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
            while ((row = TDengine.FetchRows(res)) != IntPtr.Zero)
            {
                List<TDengineMeta> metaList = TDengine.FetchFields(res);
                int numOfFiled = TDengine.FieldCount(res);

                List<String> dataRaw = new List<string>();

                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);

B
Bo Ding 已提交
51 52 53 54 55 56 57 58 59
                    if (data == IntPtr.Zero)
                    {
                        Console.Write("NULL\t");
                        continue;
                    }
                    switch ((TDengineDataType)meta.type)
                    {
                        case TDengineDataType.TSDB_DATA_TYPE_BOOL:
                            bool v1 = Marshal.ReadByte(data) == 0 ? false : true;
60
                            Console.Write(v1.ToString() + "\t");
B
Bo Ding 已提交
61 62
                            break;
                        case TDengineDataType.TSDB_DATA_TYPE_TINYINT:
63 64
                            sbyte v2 = (sbyte)Marshal.ReadByte(data);
                            Console.Write(v2.ToString() + "\t");
B
Bo Ding 已提交
65 66 67
                            break;
                        case TDengineDataType.TSDB_DATA_TYPE_SMALLINT:
                            short v3 = Marshal.ReadInt16(data);
68
                            Console.Write(v3.ToString() + "\t");
B
Bo Ding 已提交
69 70 71
                            break;
                        case TDengineDataType.TSDB_DATA_TYPE_INT:
                            int v4 = Marshal.ReadInt32(data);
72
                            Console.Write(v4.ToString() + "\t");
B
Bo Ding 已提交
73 74 75
                            break;
                        case TDengineDataType.TSDB_DATA_TYPE_BIGINT:
                            long v5 = Marshal.ReadInt64(data);
76
                            Console.Write(v5.ToString() + "\t");
B
Bo Ding 已提交
77 78 79
                            break;
                        case TDengineDataType.TSDB_DATA_TYPE_FLOAT:
                            float v6 = (float)Marshal.PtrToStructure(data, typeof(float));
80
                            Console.Write(v6.ToString() + "\t");
B
Bo Ding 已提交
81 82 83
                            break;
                        case TDengineDataType.TSDB_DATA_TYPE_DOUBLE:
                            double v7 = (double)Marshal.PtrToStructure(data, typeof(double));
84
                            Console.Write(v7.ToString() + "\t");
B
Bo Ding 已提交
85 86
                            break;
                        case TDengineDataType.TSDB_DATA_TYPE_BINARY:
87
                            string v8 = Marshal.PtrToStringUTF8(data, colLengthArr[i]);
B
Bo Ding 已提交
88 89 90 91
                            Console.Write(v8 + "\t");
                            break;
                        case TDengineDataType.TSDB_DATA_TYPE_TIMESTAMP:
                            long v9 = Marshal.ReadInt64(data);
92
                            Console.Write(v9.ToString() + "\t");
B
Bo Ding 已提交
93 94
                            break;
                        case TDengineDataType.TSDB_DATA_TYPE_NCHAR:
95
                            string v10 = Marshal.PtrToStringUTF8(data, colLengthArr[i]);
B
Bo Ding 已提交
96 97
                            Console.Write(v10 + "\t");
                            break;
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
                        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;
B
Bo Ding 已提交
121
                    }
122

B
Bo Ding 已提交
123 124 125 126 127
                }
                Console.WriteLine();
            }
            if (TDengine.ErrorNo(res) != 0)
            {
128
                Console.WriteLine($"Query is not complete, Error {TDengine.ErrorNo(res)} {TDengine.Error(res)}");
B
Bo Ding 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
            }
            // exit
            TDengine.FreeResult(res);
            TDengine.Close(conn);
            TDengine.Cleanup();
        }
        static IntPtr GetConnection()
        {
            string host = "localhost";
            short port = 6030;
            string username = "root";
            string password = "taosdata";
            string dbname = "power";
            var conn = TDengine.Connect(host, username, password, dbname, port);
            if (conn == IntPtr.Zero)
            {
                Console.WriteLine("Connect to TDengine failed");
                System.Environment.Exit(0);
            }
            else
            {
                Console.WriteLine("Connect to TDengine success");
            }
            return conn;
        }
    }
}

// output:
// Connect to TDengine success
// fieldCount=6
// ts      current voltage phase   location        groupid
// 1648432611249   10.3    219     0.31    Beijing.Chaoyang        2
// 1648432611749   12.6    218     0.33    Beijing.Chaoyang        2