Search code examples
c#vb.netrecordsetin-memory

Which class can I use to pass a "RecordSet" between an WinForm VB.Net EXE program and a classes DLL?


I have developped a VB.Net DLL that can read Postgres, Oracle and OleDb.

To allow this DLL to execute some SQL commands, I pass it a Connection object and I use it to execute some SQL SELECT command.

My problem now is that I want to pass all data contained in a GridView object that contains data obtained using a SQL command in EXE program (not in DLL) and that user can have changed.

Which type of object can I pass considering following constraints ?

  1. argument passed cannot be a WinForm control as DataGridView (no Winform object in DLL)
  2. another database's table cannot be filled with DataGridView content (not performant)
  3. class used must be part of an .Net assembly (not a COM, too old technology)

Until now, the only solution found consist of using ADO or Microsoft ActiveX Data Object Record Set, but this class is not an .Net assembly and solution found on DataBase Journal does'nt work because Items property of Field class is Read Only.

Here is my code

    Dim rstADO As ADODB.Recordset
    rstADO = New ADODB.Recordset
    With rstADO
        .Fields.Append("EmployeeID", ADODB.DataTypeEnum.adInteger, 0, ADODB.FieldAttributeEnum.adFldKeyColumn)
        .Fields.Append("FirstName", ADODB.DataTypeEnum.adVarChar, 10, ADODB.FieldAttributeEnum.adFldMayBeNull)

        .CursorType = ADODB.CursorTypeEnum.adOpenKeyset
        .CursorLocation = ADODB.CursorLocationEnum.adUseClient
        .LockType = ADODB.LockTypeEnum.adLockPessimistic
        .Open()

        For Each row As DataGridViewRow In grid.Rows
            .AddNew()
            For i = 0 To grid.Columns.Count - 1
                .Fields(i) = row.Cells(i).Value
            Next i
            .Update()
        Next row
    End With

Line .Fields(i) dispay following error BC30526: 'Item' property is 'ReadOnly'.

Which class can I use ?


Solution

  • You can use a .Net DataTable object.

    Here is some lines of code

    Dim dt As New DataTable
    
    dt.Columns.Add("EmployeeID", GetType(Integer))
    dt.Columns.Add("FirstName", GetType(String))
    
    For Each row As DataGridViewRow In grid.Rows
        dt.Rows.Add(row.Cells(0), row.Cells(1))
    Next row
    

    and when it is filled, you can pass it to your DLL

    SetDataTable(dt)