Error moving function from vb6 to VB.NET

0

I am moving a function that is programmed in VB6.

Public Function funPeriodoActual() As String
    Dim strCadena As String
    Dim strConexion As String
    Private cnConexion As ADODB.Connection
    Private rs As ADODB.Recordset
    Private strBD As String
    Dim strActual As String

    strActual = "0"

    strBD = "C:\Users\Usuario\Desktop\BC_PM.mdb"
    strConexion = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strBD & ";Persist Security Info=FALSE;Jet OLEDB"

    Set cnConexion = New ADODB.Connection
    cnConexion.ConnectionString = strConexion
    cnConexion.Open

    Set rs = New ADODB.Recordset
    Set rs.ActiveConnection = cnConexion

    rs.Open "qryUltimas_dos_BD", cnConexion, adOpenKeyset, adLockOptimistic, adCmdTable     
    rs.MoveFirst
    strActual = rs.Fields("PK_HD") 
    rs.Close

    Set rs = Nothing
    cnConexion.Close
    Set cnConexion = Nothing
    funPeriodoActual = strActual
End Function

My Code for VB.NET

Public Function funPeriodoActual() As String
        'Dim strCadena As String
        'Dim strConexion As String
        Dim rs As OleDb.OleDbDataReader
        Dim strActual As String
        Dim oleComand As OleDb.OleDbCommand

        strActual = "0"
        strBD = "C:\Users\Usuario\Desktop\BC_PM.mdb"
        cnConexion = New OleDb.OleDbConnection
        cnConexion.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & strBD & "';Jet OLEDB:Database;"
        cnConexion.Open()
        oleComand = New OleDb.OleDbCommand("qryUltimas_dos_BD", cnConexion)
        rs = oleComand.ExecuteReader()

        strActual = CStr(rs.Item("PK_HD"))
        rs.Close()    
        rs = Nothing
        cnConexion.Close()    
        Return strActual
 End Function

When executing the function, I get the error:

  

The format of the initialization string does not conform to the   specification that starts in the index 82.

I'm still wrongly transferring the data. I appreciate your comments.

    
asked by SdeSistemas 30.10.2018 в 19:23
source

1 answer

0

Just to complement ...

The connection chains for OLEDB can be found here: link

On your connection line:

  

cnConexion.ConnectionString="Provider = Microsoft.Jet.OLEDB.4.0; Data Source = '" & strBD & "'; Jet OLEDB: Database;"

I think it's over the simple quote (Source = '"....) to put the address.

As a suggestion, I recommend using the .NET ConnectionString.config to store your connection information.

    
answered by 21.01.2019 в 15:42