I'm a newby in SilverStripe, I'm running SilverStripe on MySQL and I need to get some data from external MSSQL database. Can anybody give me a recipe or example code how todo it? I have read post http://www.silverstripe.org/community/forums/general-questions/show/9061 , but it didn't help me.
We've moved the forum!
Please use forum.silverstripe.org for any new questions
(announcement).
The forum archive will stick around, but will be read only.
You can also use our Slack channel
or StackOverflow to ask for help.
Check out our community overview for more options to contribute.
Install the MSSQL adaptor: http://addons.silverstripe.org/add-ons/silverstripe/mssql
Open a connection passing a valid config. Do not forget to name the connection!
Use the connection.
DB::connect([
'type' => 'MSSQLDatabase',
//options as described: https://github.com/silverstripe/silverstripe-mssql/blob/master/code/MSSQLDatabase.php#L89-L95
], 'MSSQL connection');
DB::getConn('MSSQL connection')->query($sql);
It's fine! Thank you Nightjar for your clear and simple explanation. It works!
It should pay to note that because you're executing SQL directly you'll need to beware of normal security measures against injection, etc.
There may be a way to help avoid this through use of SQLQuery or the like, but from memory it's a bit of a pain - You must build the query, then extract the raw SQL from the class, then execute it separately. ie. DB::getConn('yourconn')->query($sqlquery->sql()); (so basically you don't get the full advantage of the abstraction class when it comes to execution).
Thank you for signficant notice.