(Solved) – How to read data from SAP database via SQL in C#


I have the task to refurbish a very old VBA macro into a .NET program. This macro is collecting data from our SAP database. Unfortunately I don’t know anything about SAP and how to deal with it from my C# program. This is the old code:

Option Private Module
Global db_005SAPTABLES As DAO.Database

Public Sub ECadSQL_Abfrage(ByVal lngZeile As Long)

Dim rsETV As DAO.Recordset
Dim MatNr As String
Dim strSQL As String

'MatNr einlesen
MatNr = Range("E" & lngZeile).Value

If MatNr = "" Then
    Range("F" & lngZeile).Value = ""
    Range("G" & lngZeile).Value = ""
    Range("H" & lngZeile).Value = ""
    Range("I" & lngZeile).Value = ""
    Range("J" & lngZeile).Value = ""
    Range("K" & lngZeile).Value = ""
GoTo weiter1
End If

'SQL abfrage
strSQL = "select * FROM T_ECAD_SAP_Daten where ArticleNumber = '" & MatNr & "'"

'Verbinden mit Datenbank

Set rsETV = db_005SAPTABLES.OpenRecordset(strSQL)

'Daten auslesen
    Range("F" & lngZeile).Value = rsETV.Fields("PP_Status").Value
    Range("G" & lngZeile).Value = rsETV.Fields("Class").Value
    Range("H" & lngZeile).Value = rsETV.Fields("Gerätetyp").Value
    Range("I" & lngZeile).Value = rsETV.Fields("Kenngroesse_1").Value
    Range("J" & lngZeile).Value = rsETV.Fields("Kenngroesse_2").Value
    Range("K" & lngZeile).Value = rsETV.Fields("Kenngroesse_3").Value


End Sub

Sub Connect_005SAPTABLES()

Dim Fehlermeldung As String
Dim constr As String

On Error GoTo Connect_005SAPTABLESErrorHandler

'Verbindungsangaben zur Datenbank
constr = "DRIVER={SQL Server};SERVER=bngdbmat.patec.group;"
constr = constr & "DATABASE=005SAPTables;UID=005SAPTables_R;PWD=sAptar;APP=F-Kalkulation"

'Datenbank öffnen
Set db_005SAPTABLES = OpenDatabase("005SAPTABLES", dbDriverNoPrompt, True, constr)

Exit Sub


Fehlermeldung = "Unbehandelter Fehler in Prozedur Connect_005SAPTABLES aufgetreten " & Chr(13) & "FehlerNr. " & Err.Number & " " & Err.Description
MsgBox Fehlermeldung, vbExclamation, "Fehlermeldung"

End Sub

Maybe there is the possibility to translate this code into C# based on actual technology. The most easiest way to get (readonly) data from that DB, will be great for me.

Thank you in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *