Como pasar un RecordSet a DataTable SAP B1 C#

Muchas veces me ha tocado realizar consultas a querys ya armadas, o mostrar los datos en un gridview, para no realizar conversión de objetos o reinventar la ruda, se me asía mas fácil pasar todo el recorset a un datatable y trabajar como siempre lo hacia con este objeto.

public DataTable RsTODataTablaV2(ref SAPbobsCOM.Recordset _rs)
{
    DataTable dt = new DataTable();
    for (int i = 0; i < _rs.Fields.Count; i++)
        dt.Columns.Add(_rs.Fields.Item(i).Description);
    while (!_rs.EoF)
    {
        DataRow row = dt.NewRow();
        for (int i = 0; i < _rs.Fields.Count; i++)
            row[i] = _rs.Fields.Item(i).Value;
        dt.Rows.Add(row.ItemArray);
        _rs.MoveNext();
    }
    return dt;
}

//esta es la version original
public DataTable RsTODataTabla(ref SAPbobsCOM.Recordset _rs)
{
	DataTable dt = new DataTable();
	for (int i = 0; i < _rs.Fields.Count; i++)
		dt.Columns.Add(_rs.Fields.Item(i).Description);
	while (!_rs.EoF)
	{
		object[] array = new object[_rs.Fields.Count];
		for (int i = 0; i < _rs.Fields.Count; i++)
			array[i] = _rs.Fields.Item(i).Value;
		dt.Rows.Add(array);
		_rs.MoveNext();
	}
	return dt;
}

4 Me gusta

Tengo tanto tiempo sin programar…
Ver este código me trajo recuerdos hermosos de esa época…

Aqui va algo parecido pero convirtiendo un SAPbouiCOM.DataTable en un DataTable .Net, yo tengo estas distintas funciones como metodos de extension de la clase DataTable, con los que puedo llamarlos como metodos en cualquier instancia de dicho objeto.

        Public Function ConvertirDataTableSAP(ByVal SAPDataTable As SAPbouiCOM.DataTable) As DataTable

            '\ This function will take an SAP DataTable from the SAPbouiCOM library and convert it to a more
            '\ easily used ADO.NET datatable which can be used for data binding much easier.

            Dim dtTable As New DataTable
            Dim NewCol As DataColumn
            Dim NewRow As DataRow
            Dim ColCount As Integer

            Try

                For ColCount = 0 To SAPDataTable.Columns.Count - 1
                    NewCol = New DataColumn(SAPDataTable.Columns.Item(ColCount).Name)
                    dtTable.Columns.Add(NewCol)
                Next

                For i = 0 To SAPDataTable.Rows.Count - 1

                    NewRow = dtTable.NewRow
                    'populate each column in the row we're creating
                    For ColCount = 0 To SAPDataTable.Columns.Count - 1
                        NewRow.Item(SAPDataTable.Columns.Item(ColCount).Name) = SAPDataTable.GetValue(ColCount, i)
                    Next

                    'Add the row to the datatable
                    dtTable.Rows.Add(NewRow)
                Next

                Return dtTable

            Catch ex As Exception
                MsgBox(ex.ToString & Chr(10) & "Error converting SAP DataTable to DataTable .Net", MsgBoxStyle.Exclamation)
                ConvertirDataTableSAP = Nothing
                Exit Function
            End Try

        End Function
3 Me gusta