示例#1
0
        public DataTable GetSchemaTable()
        {
            PropertyDataCollection properties = null;

            if (_firstObject == null)
            {
                var moveNext = _enumerator.MoveNext();
                _firstRead = true;

                if (moveNext)
                {
                    _firstObject = _enumerator.Current;
                    properties   = _firstObject.Properties;
                }
                else
                {
                    var    query     = _command.CommandText;
                    var    index     = 0;
                    var    fromFound = false;
                    string className = null;

                    while (true)
                    {
                        var wordStart = QueryTextBox.NextWordStart(query, index);
                        var wordEnd   = QueryTextBox.WordEnd(query, wordStart);
                        var word      = query.Substring(wordStart, wordEnd - wordStart + 1);

                        if (fromFound)
                        {
                            className = word;
                            break;
                        }
                        else
                        {
                            if (word.ToLower() == "from")
                            {
                                fromFound = true;
                            }

                            index = wordEnd + 1;
                        }
                    }

                    query = $"SELECT * FROM meta_class WHERE __this ISA '{className}'";
                    var objectQuery = new ObjectQuery(query);
                    var searcher    = new ManagementObjectSearcher(_command.Connection.Scope, objectQuery);
                    var objects     = searcher.Get();
                    var enumerator2 = objects.GetEnumerator();
                    enumerator2.MoveNext();
                    properties = enumerator2.Current.Properties;
                }
            }

            var schemaTable = new DataTable();

            schemaTable.Columns.Add("ColumnName", typeof(string));
            schemaTable.Columns.Add("ColumnSize", typeof(int));
            schemaTable.Columns.Add("DataType", typeof(Type));
            schemaTable.Columns.Add("ProviderType", typeof(int));
            schemaTable.Columns.Add("ProviderTypeStr", typeof(string));
            schemaTable.Columns.Add("NumericPrecision", typeof(int));
            schemaTable.Columns.Add("NumericScale", typeof(int));
            schemaTable.Columns.Add("IsArray", typeof(bool));
            var values = new object[8];

            foreach (var propertyData in properties)
            {
                var  cimType = propertyData.Type;
                Type dataType;
                int  size;

                switch (cimType)
                {
                case CimType.Boolean:
                    dataType = typeof(bool);
                    size     = 1;
                    break;

                case CimType.DateTime:
                    dataType = typeof(DateTime);
                    size     = 8;
                    break;

                case CimType.UInt8:
                    dataType = typeof(byte);
                    size     = 1;
                    break;

                case CimType.UInt16:
                    dataType = typeof(ushort);
                    size     = 2;
                    break;

                case CimType.UInt32:
                    dataType = typeof(uint);
                    size     = 4;
                    break;

                case CimType.UInt64:
                    dataType = typeof(ulong);
                    size     = 8;
                    break;

                case CimType.String:
                    dataType = typeof(string);
                    size     = 0;
                    break;

                default:
                    dataType = typeof(object);
                    size     = 0;
                    break;
                }

                int    providerType;
                string providerTypeStr;

                if (propertyData.IsArray)
                {
                    providerType = (int)cimType | 0x1000;
                    var typeName = dataType.FullName + "[]";
                    dataType        = Type.GetType(typeName);
                    providerTypeStr = cimType.ToString() + "[]";
                }
                else
                {
                    providerType    = (int)cimType;
                    providerTypeStr = cimType.ToString();
                }

                values[0] = propertyData.Name;
                values[1] = size;
                values[2] = dataType;
                values[3] = providerType;
                values[4] = providerTypeStr;
                values[7] = propertyData.IsArray;
                schemaTable.Rows.Add(values);
            }

            FieldCount = schemaTable.Rows.Count;

            return(schemaTable);
        }