コード例 #1
0
        /// <summary>
        /// transfer data to datatable
        /// </summary>
        /// <returns></returns>
        public DataTable toDataTable()
        {
            DataTable dt = new DataTable(_tableName);

            foreach (string fieldName in names_)
            {
                int        i     = name2index_[fieldName].Value;
                DATA_TYPE  dtype = columns_[i].getDataType();
                DataColumn dc    = new DataColumn(fieldName, Utils.getSystemType(dtype));
                dt.Columns.Add(dc);
            }
            if (columns_.Count == 0)
            {
                return(null);                    //table columns not exists
            }
            int rowCount = columns_[0].rows();

            for (int rowIndex = 0; rowIndex < this.rows(); rowIndex++)
            {
                DataRow dr = dt.NewRow();

                for (int colIndex = 0; colIndex < this.columns(); colIndex++)
                {
                    DATA_TYPE dtype = columns_[colIndex].getDataType();
                    switch (dtype)
                    {
                    case DATA_TYPE.DT_BOOL:
                        BasicBoolean bbobj = (BasicBoolean)columns_[colIndex].get(rowIndex);
                        if (bbobj.isNull())
                        {
                            dr[colIndex] = DBNull.Value;
                        }
                        else
                        {
                            dr[colIndex] = bbobj.getValue();
                        }
                        break;

                    case DATA_TYPE.DT_BYTE:
                        BasicByte bobj = (BasicByte)columns_[colIndex].get(rowIndex);
                        if (bobj.isNull())
                        {
                            dr[colIndex] = DBNull.Value;
                        }
                        else
                        {
                            dr[colIndex] = bobj.getValue();
                        }
                        break;

                    case DATA_TYPE.DT_SHORT:
                        BasicShort bsobj = (BasicShort)columns_[colIndex].get(rowIndex);
                        if (bsobj.isNull())
                        {
                            dr[colIndex] = DBNull.Value;
                        }
                        else
                        {
                            dr[colIndex] = bsobj.getValue();
                        }
                        break;

                    case DATA_TYPE.DT_INT:
                        BasicInt biobj = (BasicInt)columns_[colIndex].get(rowIndex);
                        if (biobj.isNull())
                        {
                            dr[colIndex] = DBNull.Value;
                        }
                        else
                        {
                            dr[colIndex] = biobj.getValue();
                        }
                        break;

                    case DATA_TYPE.DT_LONG:
                        BasicLong blobj = (BasicLong)columns_[colIndex].get(rowIndex);
                        if (blobj.isNull())
                        {
                            dr[colIndex] = DBNull.Value;
                        }
                        else
                        {
                            dr[colIndex] = blobj.getValue();
                        }
                        break;

                    case DATA_TYPE.DT_FLOAT:
                        BasicFloat bfobj = (BasicFloat)columns_[colIndex].get(rowIndex);
                        if (bfobj.isNull())
                        {
                            dr[colIndex] = DBNull.Value;
                        }
                        else
                        {
                            dr[colIndex] = bfobj.getValue();
                        }
                        break;

                    case DATA_TYPE.DT_DOUBLE:
                        BasicDouble bdobj = (BasicDouble)columns_[colIndex].get(rowIndex);
                        if (bdobj.isNull())
                        {
                            dr[colIndex] = DBNull.Value;
                        }
                        else
                        {
                            dr[colIndex] = bdobj.getValue();
                        }
                        break;

                    default:
                        IScalar eobj = (IScalar)columns_[colIndex].get(rowIndex);
                        if (eobj.isNull())
                        {
                            dr[colIndex] = DBNull.Value;
                        }
                        else
                        {
                            dr[colIndex] = eobj.getString();
                        }
                        break;
                    }
                }
                dt.Rows.Add(dr);
            }
            return(dt);
        }