Пример #1
0
        private List <TSDB_FIELD> FetchFields(long taos)
        {
            const int fieldSize = 68;

            List <TSDB_FIELD> fields = new List <TSDB_FIELD>();
            long result = taos_use_result(taos);

            if (result == 0)
            {
                return(fields);
            }
            int    fieldCount = taos_field_count(taos);
            IntPtr fieldsPtr  = taos_fetch_fields(result);

            for (int i = 0; i < fieldCount; ++i)
            {
                int        offset = i * fieldSize;
                TSDB_FIELD meta   = new TSDB_FIELD();
                meta.name  = Marshal.PtrToStringAnsi(fieldsPtr + offset);
                meta.bytes = Marshal.ReadInt16(fieldsPtr + offset + 64);
                meta.type  = Marshal.ReadByte(fieldsPtr + offset + 66);
                fields.Add(meta);
            }
            return(fields);
        }
Пример #2
0
        /// <summary>
        /// SQl查询返回System.Data.DataTable
        /// </summary>
        /// <param name="sql">SQl查询语句</param>
        /// <returns></returns>
        public DataTable QueryToDataTable(string sql)
        {
            DataTable dt = new DataTable();

            if (_taosConn == 0)
            {
                return(new DataTable());
            }
            try
            {
                if (SQLCommand(sql) == TSDB_CODE.SUCCESS)
                {
                    int fieldCount = TDengine.taos_field_count(_taosConn);

                    Console.WriteLine("field count: " + fieldCount);

                    List <TSDB_FIELD> fields = FetchFields(_taosConn);
                    foreach (var f in fields)
                    {
                        dt.Columns.Add(f.name, f.CSharpTypeName());
                        #if (DEBUG)
                        Console.WriteLine(string.Format("name:{0} type:{1} name:{2} bytes:{3}", f.name.PadRight(20), f.type.ToString().PadRight(5), f.TSDBDataTypeName().PadRight(10), f.bytes.ToString().PadRight(5)));
                        #endif
                    }

                    long result = taos_use_result(_taosConn);
                    if (result == 0)
                    {
                        Console.WriteLine(sql + " result set is null");
                        return(dt);
                    }
                    IntPtr rowdata;
                    long   queryRows = 0;
                    while ((rowdata = taos_fetch_row(result)) != IntPtr.Zero)
                    {
                        queryRows++;
                        DataRow dr = dt.NewRow();
                        Console.Write(queryRows + "\t");
                        for (int index = 0; index < fieldCount; ++index)
                        {
                            TSDB_FIELD field  = fields[index];
                            int        offset = 8 * index;

                            IntPtr data = Marshal.ReadIntPtr(rowdata, offset);



                            if (data == IntPtr.Zero)
                            {
                                Console.Write("NULL");
                                continue;
                            }
                            dynamic v = new object();
                            switch ((TSDB_DATA_TYPE)field.type)
                            {
                            case TSDB_DATA_TYPE.BOOL:
                                //bool v1 = Marshal.ReadByte(data) == 0 ? false : true;
                                //Console.Write(v1);
                                v = Marshal.ReadByte(data) == 0 ? false : true;
                                break;

                            case TSDB_DATA_TYPE.TINYINT:
                                // byte v2 = Marshal.ReadByte(data);
                                //Console.Write(v2);
                                v = Marshal.ReadByte(data);
                                break;

                            case TSDB_DATA_TYPE.SMALLINT:
                                //short v3 = Marshal.ReadInt16(data);
                                //Console.Write(v3);
                                v = Marshal.ReadInt16(data);
                                break;

                            case TSDB_DATA_TYPE.INT:
                                //int v4 = Marshal.ReadInt32(data);
                                //Console.Write(v4);
                                v = Marshal.ReadInt32(data);
                                break;

                            case TSDB_DATA_TYPE.BIGINT:
                                //long v5 = Marshal.ReadInt64(data);
                                //Console.Write(v5);
                                v = Marshal.ReadInt64(data);
                                break;

                            case TSDB_DATA_TYPE.FLOAT:
                                //float v6 = (float)Marshal.PtrToStructure(data, typeof(float));
                                //Console.Write(v6);
                                v = (float)Marshal.PtrToStructure(data, typeof(float));
                                break;

                            case TSDB_DATA_TYPE.DOUBLE:
                                //double v7 = (double)Marshal.PtrToStructure(data, typeof(double));
                                //Console.Write(v7);
                                v = (double)Marshal.PtrToStructure(data, typeof(double));
                                break;

                            case TSDB_DATA_TYPE.BINARY:
                                //string v8 = Marshal.PtrToStringAnsi(data);
                                //Console.Write(v8);
                                v = Marshal.PtrToStringAnsi(data);
                                break;

                            case TSDB_DATA_TYPE.TIMESTAMP:
                                long            v9        = Marshal.ReadInt64(data);
                                System.DateTime time      = System.DateTime.MinValue;
                                System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0));
                                time = startTime.AddMilliseconds(v9);
                                //Console.Write(Convert.ToDateTime(time));
                                v = time;
                                break;

                            case TSDB_DATA_TYPE.NCHAR:
                                //string v10 = Marshal.PtrToStringAnsi(data);
                                //Console.Write(v10);
                                v = Marshal.PtrToStringAnsi(data);
                                break;
                            }
                            Console.Write(v + "\t");
                            dr[index] = v;
                        }
                        Console.WriteLine("");
                        dt.Rows.Add(dr);
                    }

                    if (taos_errno(_taosConn) != 0)
                    {
                        Console.WriteLine("Query is not complete, Error {0:G}", taos_errno(_taosConn), Error(_taosConn));
                    }
                    else
                    {
                        Console.WriteLine("Query is complete");
                    }
                    taos_free_result(result);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(dt);
        }