none
ADODB - Get value from a query and save it to a String RRS feed

  • Question

  • I'm learning to adapt code that I use in VBA to work in VB.Net, And I'm having an hard time to figure how to get a value from a query and store it in a String.

    I think the connection is working well but unlikne VBA when I try to store the value of a field in the String "Valor", an error happens: 

    System.TypeLoadException: 'Could not load type 'ADODB.FieldsToInternalFieldsMarshaler' from assembly 'MailService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.'

        Sub Main()
    
            Dim VD As New ADODB.Connection
            Dim RS As New ADODB.Recordset
            Dim Valor As String
    
            VD.ConnectionString = "DSN=XXX;UID=XXX;PWD=XXX;"
    
            VD.Open()
            RS = VD.Execute("SELECT Cod_PA FROM Tab_SERVICO WHERE Cod_PA='VIADELMAR'")
            If Not (RS.BOF And RS.EOF) Then
                Valor = RS.Fields("Cod_PA").Value
            Else
                Valor = "Não existe!"
            End If
            RS.Close()
            VD.Close()
    
            MsgBox(Valor)
    
        End Sub



    Tuesday, June 18, 2019 6:32 PM

All replies

  • Hello,

    A better option is to use OleDb data provider, it requires a good deal more code but worth it.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, June 18, 2019 7:39 PM
    Moderator
  • Hi Karen,

    In my case I had to use Odbc, I understood that with the help of user tgolisch @ StackOverFlow.

    In the end, this is what I got working for me:

            Dim VD As New System.Data.Odbc.OdbcConnection
            Dim CMD As New System.Data.Odbc.OdbcCommand
            Dim Valor As String
    
            VD.ConnectionString = "Provider=MSDASQL;DSN=XXX;UID=XXX;PWD=XXX;"
            VD.Open()
    
            CMD.Connection = VD
            CMD.CommandText = "SELECT Cod_PA FROM Tab_SERVICO WHERE Cod_PA='VIADELMAR'"
    
            Dim DT As New DataTable
            Dim DA As New System.Data.Odbc.OdbcDataAdapter(CMD)
            DA.Fill(DT)
    
            If (DT.Rows.Count > 0) Then
                Valor = DT.Rows(0)("Cod_PA")
            Else
                Valor = "Não existe!"
            End If
    
            DA.Dispose()
    
            CMD.Dispose()
            VD.Close()
            VD.Dispose()
    
            MsgBox(Valor)


    • Edited by DelmarPT Wednesday, June 19, 2019 12:04 PM took out a not needed line (debug.print)
    Wednesday, June 19, 2019 11:45 AM
  • Hi,

    Do you resolve the issue?It would be appreciated if you  mark your solution as an answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 24, 2019 1:24 AM
    Moderator