Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Using external MSSQL database


Go to End


5 Posts   942 Views

Avatar
alex_sm

Community Member, 19 Posts

26 May 2015 at 8:56pm

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.

Avatar
Nightjar

Community Member, 28 Posts

26 May 2015 at 11:02pm

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);

Avatar
alex_sm

Community Member, 19 Posts

27 May 2015 at 12:41am

It's fine! Thank you Nightjar for your clear and simple explanation. It works!

Avatar
Nightjar

Community Member, 28 Posts

27 May 2015 at 12:54am

Edited: 27/05/2015 12:55am

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).

Avatar
alex_sm

Community Member, 19 Posts

27 May 2015 at 1:57am

Thank you for signficant notice.