Alles erfolgreiche in der Programmierwelt bekommt von Microsoft eine eigene Antwort. Meist völlig unnötigerweise, aber das soll nicht Kern dieses Artikels werden.
So war Microsofts Antwort auf das SQLite Datenbankformat die „SQL Server Compact Edition„, die .sdf Dateien ausspuckt. Vor allem in der .NET Welt soll dieses Format zum Einsatz kommen und erste Anwendungen enstehen, die statt auf Access Dateien die sdf Datenbanken verwenden. Auch ich bekam neulich eine Anfrage bei divelogs.de ob ich nicht die neue Mares Software „Dive Organizer“ unterstützen könne. Kurz ein Blick darauf geworfen und das sdf Format entdeckt. Mares hatte bisher nur Access .mdb Dateien verwendet.
Die lange Suche nach einer Auslesemöglichkeit von SQL Compact Edition Dateien mit PHP brachte keine Ergebnisse, aber viele Fragen danach. Eine PHP Erweiterung dafür gibt es (bisher) nicht, daher musste eine andere Lösung her. Ich habe die Klasse class_mdb von Peter Klauer (knito.de), die Datenbankanfragen an mdb Dateien an die COM Schnittstelle von Windows weiterreicht umgeschrieben, damit die mit sdf Dateien umgehen kann. Dies bedeutet allerdings auch, daß die Lösung nur auf Windows Servern mit installierter SQL Server Compact Edition funktioniert. Dazu muss die Datenbankengine von diesem Link noch installiert werden. Danach steht die SQL Compact Edition als Data Provider zur Verfügung.
Ist die Klasse eingebunden, ist die Abfrage denkbar einfach:
<!--?php // Example script for class_sdf include("class_sdf.php"); $sdf = new sdf("testdatabase.sdf", "password@1"); $sdf--->execute("SELECT id, text FROM test"); while( !$sdf->eof() ) { echo "The row with the id ".$sdf->fieldvalue('id')." has the value: ".$sdf->fieldvalue('text'); $sdf->movenext(); } $sdf->close(); ?>; |
Damit auch andere davon profitieren, stelle ich die Klasse class_sdf mit Beispielskript und Beispieldatenbank hier zum Download zur Verfügung:
Die Verwendung erfolgt auf eigene Gefahr, ohne jegliche Gewähr und ohne Support meinerseits. Wenn es jemandem hilft, freue ich mich über einen Kommentar auf dieser Seite 🙂
Thanks very much for the class. It works very well for some SDFs. You seem to be the only source for PHP-SDF info on the web.
I have one large SDF that I can’t seem to open. Is there a change to be made to handle SDFs of size greater than 1GB?
It’s SQLCE 3.5, 939MB
Exception: exception ‚com_exception‘ with message ‚Source: Microsoft SQL Server Compact OLE DB Provider Description: Unknown‘ in C:\sdfphp\class_sdf.php:70
Stack trace:
#0 C:\sdfphp\class_sdf.php(70): com->open(‚Provider=Micros…‘)
#1 C:\sdfphp\class_sdf.php(52): sdf->open()
#2 C:\sdfphp\wordlinks.php(52): sdf->sdf(‚mybigsdffile…‘, ‚passsd
8…‘)
#3 C:\sdfphp\wordlinks.php(68): open_sdf()
#4 {main}
Could you possibly suggest anything?
Thank you.
Hello, I am having some problems with this code. I’d like to contact you via email
Henrique, Install the packages from this link for it to work:
http://www.microsoft.com/en-us/download/details.aspx?id=5821
Moin,
erst einmal vielen Dank für das Code-Schnipsel und die Klasse. Ich bekomme von einer Software sdf-Dateien der Version SQLCE3.1. Wie muss ich die Klasse verändern, dass auch auf diese Version der Connect funktioniert?
(Leider gibt es derzeit keine Möglichkeit, die sdf-Datei schon als 3.5 „liefern“ zu lassen. Der Hersteller betreut diese Software nur ziemlich halbherzig.)
Vielen Dank schon mal.
Hi Joe,
Keine Ahnung. Wundert mich, daß 3.5 nicht abwärtskompatiel ist… Du kannst höchstens versuchen den 3.1 Treiber von Microsoft zu installieren und den Providerstring für die Version googeln und austauschen (Variable $strProvider). Benutze bitte auch die Installationsdateien von diesem Link: http://www.microsoft.com/en-us/download/details.aspx?id=5821
Viel Erfolg!
Hi Klemmkeil,
I ran your example. but I can not run another another file.
No password in this file. The size of this file is 790 MB
Error code: „Exception: Object id #3“
Hi Klemmkeil,
Thank you for writing this class. I tried using this on SQL CE database 4.0 but I keep getting this error. „Source: Microsoft SQL Server Compact OLE DB Provider Description: Unknown“. Not sure what is wrong. I switched the provider name in the class to 4.0. Do you have any thoughts.
Thanks.
Joe, You will need to install the SQL Compact 4.0 database package from Microsoft from here: http://www.microsoft.com/de-de/download/details.aspx?id=17876
Then change the Provderstring to the following:
var $strProvider = ‚Provider=Microsoft.SQLSERVER.CE.OLEDB.4.0;‘
Works for me…
Great work. This is a life saver. You should also mention that the php extension php_com_dotnet.dll should be enabled in the php.in file. This can save some troubleshooting stress for someone tomorrow. Great work again. Many thanks.
Right.
– Extension ‚php_com_dotnet.dll‘ must be activated (check php version first)
– Windows SQL Compact Server 3.5 or 4.0 SP1 must be installed on your Windows Server (mine is 2012 R2)
– Don’t forget to change ‚Provider=Microsoft.SQLSERVER.CE.OLEDB.x.x‘ with yours. 3.5 or 4.0 into ‚class_sdf.php‘
and..
..everything is working fine. Even 8 years after the first script in 2011. It’s now September 2019 !!
Good job, Rainer. Big one applause man.. You’re the ONLY resource on the whole internet to give THE solution to open and read SDF database.
Thanks Falken!
Your comments are all correct, thanks for the clarification.
Seems to be a pretty uncommon task to read SDF files via PHP…
Sadly, it only works on Windows servers, as there is no php_com_dotnet for any other operating system, and no SQLSERVER Compact edition either…
Regards, Rainer
Great work! I am running into one issue. This works fine if I run it through PHP using the command prompt. However, it fails when use it with IIS:
Exception: com_exception: Source: Microsoft SQL Server Compact OLE DB Provider
Description: Unknown in C:\inetpub\wwwroot\AccessManager\Reports\MSSI\includes\class_sdf.php:77Stack trace: #0 C:\inetpub\wwwroot\AccessManager\Reports\MSSI\includes\class_sdf.php(77): com->open(‚Provider=Micros…‘) #1 C:\inetpub\wwwroot\AccessManager\Reports\MSSI\includes\class_sdf.php(52): sdf->open() #2 C:\inetpub\wwwroot\AccessManager\Reports\MSSI\includes\example.php(13): sdf->__construct(‚testdatabase.sd…‘, “) #3 {main}
Any thoughts?!? Thanks!
Hi Dan,
I’ve never used IIS, so no idea. Sorry…
Rainer
i need urgent help im facing problem with open sdf file larger than 256 MB
Hi, sorry, but I can’t give support, the class is provided as is
Rainer