Laravel SQL server connection

sf6xfgos  于 2023-03-07  发布在  SQL Server
关注(0)|答案(1)|浏览(152)

I have laravel 7.3, and sql server 16,

i need to connect to the database sql server, but i have this error :

SQLSTATE = 08001 error- No connection could be made because the target machine actively refused it.

and there is the .env file

DB_CONNECTION=sqlsrv
DB_HOST=127.0.0.1/instancename
DB_PORT=1433
DB_DATABASE=Database
DB_USERNAME=sa
DB_PASSWORD=12345678`

But when i use the simple php file to connect the connexion is ok.

this is the php script :

<?php 
$serverName = "127.0.0.1/instancename";
$connectionInfo = array( "Database"=>"Database", "UID"=>"sa", "PWD"=>"12345678");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
echo "Connexion établie.<br />";
}else{
echo "La connexion n'a pu être établie.<br />";
die( print_r( sqlsrv_errors(), true));

}
?>

Manny thanks for your help

5vf7fwbs

5vf7fwbs1#

You can use this method Firstly add your sql server configuration in config/database.php

'sqlsrv' => [
        'driver' => 'sqlsrv',
        'host' => "(HOST_ADDR)",
        'port' => "(PORT)",
        'database' => "UNIS",
        'username' => "(USER_NAME)",
        'password' => "(PASSWORD)",
        'charset' => 'utf8',
        'prefix' => '',
        'strict' => false,
    ]

Then Add Model connection as

protected $connection = 'sqlsrv';

相关问题