DataSet, DataTable
博客专区 > fandyx 的博客 > 博客详情
DataSet, DataTable
fandyx 发表于3年前
DataSet, DataTable
  • 发表于 3年前
  • 阅读 31
  • 收藏 0
  • 点赞 0
  • 评论 0

【腾讯云】新注册用户域名抢购1元起>>>   

摘要: 创建和读取DataSet, DataTable
// Copyright (C), 2015, CEU Co., Ltd.
// USR Changed by 2015-02-03T14:02:52 Fandy Xie 谢宇帆
// Version       : 1.0
// Description   : AX2009处理,读取dataSet,dataTable
// Return        : dataSet, dataTable
static System.Data.DataSet dataSet_dataTable_read(
    companyId   _companyId,
    tableName   _tableName,
    fieldName   _fieldName01   = "",
    anyType     _fieldValue1   = null,
    fieldName   _fieldName02   = '',
    anyType     _fieldValue2   = null,
    fieldName   _fieldName03   = "",
    anyType     _fieldValue3   = null,
    fieldName   _fieldName04   = '',
    anyType     _fieldValue4   = null,
    int         _maxRecords    = 2)
{
    System.Data.DataTable               dataTable;
    System.Data.DataSet                 dataSet;
    System.Data.DataTableCollection     dtCollection;
    System.Data.DataColumnCollection    colCollection;
    System.Data.DataRowCollection       rowCollection;
    System.Data.DataTableReader         dataTableReader;
    System.Data.DataRow                 row;
    System.Data.DataColumn              column;

    Common                  common;
    DictTable               dictTable;
    query                   q;
    queryRun                qr;
    queryBuildRange         qbr;
    queryBuildDataSource    qbds;
    map                     mapField = new map(types::Integer, types::String);
    MapEnumerator           me;
    fieldId                 fieldId, field1, field2, field3, field4;
    str                     tableName, fieldName, xmlValue;
    int                     columnCounter, rowCounter, i, j, m;
    str                     value, ret;
    container               mapCon;
    ;


    dictTable   = new DictTable(tableName2id(_tableName));
    common      = dictTable.makeRecord();
    field1      = fieldname2Id(dictTable.id(), _fieldName01);
    field2      = fieldname2Id(dictTable.id(), _fieldName02);
    field3      = fieldname2Id(dictTable.id(), _fieldName03);
    field4      = fieldname2Id(dictTable.id(), _fieldName04);

    q           = new query();
    q.allowCrossCompany(true);
    if (_companyId)
    {
        q.addCompanyRange(_companyId);
    }
    qbds        = q.addDataSource(dictTable.id());
    if (field1)
    {
        qbr     = qbds.addRange(field1);
        if (typeof(_fieldValue1) == types::String)
            qbr.value(_fieldValue1);
        else
            qbr.value(sysQuery::value(_fieldValue1));
    }
    if (field2)
    {
        qbr     = qbds.addRange(field2);
        if (typeof(_fieldValue2) == types::String)
            qbr.value(_fieldValue2);
        else
            qbr.value(sysQuery::value(_fieldValue2));
    }

    if (field3)
    {
        qbr     = qbds.addRange(field3);
        if (typeof(_fieldValue3) == types::String)
            qbr.value(_fieldValue3);
        else
            qbr.value(sysQuery::value(_fieldValue3));
    }
    if (field4)
    {
        qbr     = qbds.addRange(field4);
        if (typeof(_fieldValue4) == types::String)
            qbr.value(_fieldValue4);
        else
            qbr.value(sysQuery::value(_fieldValue4));
    }

    dataTable       = new System.Data.DataTable();
    colCollection   = dataTable.get_Columns();
    colCollection   = YIP_ViewDataSet::getTableColumnsCollection(colCollection, dictTable.id());
    mapField        = YIP_ViewDataSet::getTableFieldsMap(dictTable.id());

    rowCollection   = dataTable.get_Rows();
    tableName       = dictTable.name();
    dataTable.set_TableName(tableName);

    qr              = new queryRun(q);
    //info(qbds.toString());
    while (qr.next())
    {
        common = qr.get(dictTable.id());
        i++;
        if (_maxRecords)
        {
            if (i > _maxRecords)
                break;
        }
        row= dataTable.NewRow();
        me = mapField.getEnumerator();
        while (me.moveNext())
        {
            fieldId = me.currentKey();
            fieldName = me.currentValue();
            row.set_Item(fieldName, common.(fieldId));
        }
        rowCollection.Add(row);
    }


    setprefix(tableName);
    info("...coloumn, row Collection....................");
    rowCounter = rowCollection.get_Count();
    columnCounter = colCollection.get_Count();
    for(i = 0; i < rowCounter; i ++)
    {
        row = rowCollection.get_Item(i);
        //row01   = CLRInterop::getAnyTypeForObject(row.get_Item(i));
        for(j = 0; j < columnCounter; j ++)
        {
            column      = colCollection.get_Item(j);
            fieldName   = column.get_ColumnName();
            if (row.IsNull(j))
                xmlValue = "null";
            else
                xmlValue = row.get_Item(j);
            info(strfmt("Row Index[%1], column Index[%2, %3], value[%4].", i + 1, j + 1, fieldName, xmlValue));
        }
    }
    dataTable.WriteXml(@"C:\Users\h2xyf01\Desktop\EWF\a.xml");
    info("...dataTable.CreateDataReader....................");
    dataTableReader = dataTable.CreateDataReader();
    i = 0;

    while (dataTableReader.Read())
    {
        i++;
        value = queryValue(ClrInterop::getAnyTypeForObject(dataTableReader.GetValue(0)));
        //value = ClrInterop::getAnyTypeForObject(dataTableReader.get_Item("NAME"));
        value = strfmt("%1,%2", i,value);
        ret += ret ? "\n" + value : value;
    }
    info(ret);

    dataSet     = new System.Data.DataSet();
    dataSet.set_DataSetName(dataTable.get_TableName());
    dtCollection = dataSet.get_Tables();
    dtCollection.Add(dataTable);

    i = 0;
    info("...dataSet.CreateDataReader....................");
    dataTableReader = dataSet.CreateDataReader();
    ret = "";
    while (dataTableReader.Read())
    {
        i++;
        value = queryValue(ClrInterop::getAnyTypeForObject(dataTableReader.GetValue(0)));
        //value = ClrInterop::getAnyTypeForObject(dataTableReader.get_Item("NAME"));
        value = strfmt("%1,%2", i,value);
        ret += ret ? "\n" + value : value;
    }
    info(ret);

    return dataSet;
}


  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 0
博文 39
码字总数 11111
×
fandyx
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: