Page 1 of 1

How to connect to local SQL-Server in Visual Studio

PostPosted: July 19th, 2017, 4:14 pm
by Bluesky787
Hello everyone,

It's really a shame to post this question, but I am a complete PHP newbie and I stumbled upon thousands of different approaches to connect PHP to MSSQL, but not in Visual Studio. Apparently, this is still a bit uncommon...

So, now I have set up a MSSQL-Server from within Visual Studio 2017, where PHP Tools for Visual Studio is installed. The server is hosted locally in a .mdf file. There are already some tables created and VS tells me there's a functioning connection to MSSQL. It also gives me a connection String.

Now I tried to connect using

Code: Select all
$server_db = mysqli_connect('(localdb)\\v13.0', array('Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="D:\Users\Username\Documents\Visual Studio 2017\Projects\MyProject\SQL_DB.mdf";Integrated Security=True;Connect Timeout=30'));


where the whole string within this connect command is the given connection string I retrieved from VS (in Server-Properties).

So, conclusion is, the program runs this command and returns a null Object.

Most sources I found have stated I should use 'sqlsrv_connect' instead, but this command isn't recognized by VS.

What am I doing wrong?
Help of every kind is highly appreciated.

Regards

Re: How to connect to local SQL-Server in Visual Studio

PostPosted: July 25th, 2017, 8:39 am
by Miloslav Beno
Hello,

This question is not really PHP Tools related, but basicaly issue is that you are trying to connect to mssql server with mysql api.

As you've pointed out you will need to use sqlsrv_* functions, which are not installed by default. You will need to place php_sqlsrv.dll extension in the ext directory of your php installation annd activate the extension in php.ini.

You can get the extension from here https://www.microsoft.com/en-us/download/details.aspx?id=20098

I hope that will help.

Thanks,

Re: How to connect to local SQL-Server in Visual Studio

PostPosted: August 12th, 2017, 10:04 am
by Bluesky787
Hello Miloslav,

I could verify your answer yesterday and it worked for me.
Thanks.

Best Regards.

Re: How to connect to local SQL-Server in Visual Studio

PostPosted: August 12th, 2017, 10:49 am
by Bluesky787
If someone else has the same problem, here are some more detailed info:
The link specified by Miloslav will give you the choice of 4 different driver downloads (SQLSERVER 3.0, 3.1, 3.2 and 4.0). If you work with >=PHP 7.0, 4.0 is yours (at the time of this writing).

Extract the files to the ext folder in your PHP installation directory. You could see it by printing phpinfo() in your php project. When in doubt, standard folder in PHPTools seems to be
%SystemDrive%\Program Files (x86)\IIS Express\PHP\v7.0\ext (on x64 systems)
%SystemDrive%\Program Files\IIS Express\PHP\v7.0\ext (on x86 systems)

In php.ini File, you have to specify the ext-directory and the addons you installed and want to use. Choose the extension you need or, when in doubt, all. For me, it works with the first four extensions enabled:

Following lines must be added (without ''):
'extension_dir = "ext"'
'extension=php_sqlsrv_7_nts_x64.dll' (or)
'extension=php_sqlsrv_7_nts_x86.dll' (or)
'extension=php_sqlsrv_7_ts_x64.dll' (or)
'extension=php_sqlsrv_7_ts_x86.dll' (or)

'extension=php_pdo_sqlsrv_7_nts_x64.dll' (or)
'extension=php_pdo_sqlsrv_7_nts_x86.dll' (or)
'extension=php_pdo_sqlsrv_7_ts_x64.dll' (or)
'extension=php_pdo_sqlsrv_7_ts_x86.dll'

Now everything should be set up correctly.
Regards and happy coding :)

Edit: I am also required to download and install the Microsoft ODBC driver 11 for SQL Server, which is available here:
https://www.microsoft.com/en-us/download/confirmation.aspx?id=36434

Re: How to connect to local SQL-Server in Visual Studio

PostPosted: June 25th, 2018, 4:06 pm
by cecilwhitmore
I'm having trouble connecting to SQL server via PHP tools. I've followed the instructions in the post but still cannot connect. Any help would be much appreciated.
Cecil

Re: How to connect to local SQL-Server in Visual Studio

PostPosted: July 23rd, 2018, 11:05 am
by danielpaul
Steps to connect LocalDB to Visual Studio Server Explorer

Open command prompt
Run SqlLocalDB.exe start v11.0
Run SqlLocalDB.exe info v11.0
Copy the Instance pipe name that starts with np:\...
In Visual Studio select TOOLS > Connect to Database...
For Server Name enter (localdb)\v11.0. If it didn't work, use the Instance pipe name that you copied earlier. You can also use this to connect with SQL Management Studio.
Select the database on next dropdown list
Click OK

------------------
In order to understand what Sarcomatoid mesothelioma is the rare and also the most aggressive form of cancer. It is usually found in pleural mesothelioma. Diagnosis and treatment.