Laravel配置ODBC驱动程序

时间:2014-03-26 12:15:13

标签: sql-server laravel laravel-4 odbc

我需要配置Laravel 4以使用SQL Server 2000的ODBC PDO驱动程序

我在一个普通的PHP文件中对它进行了测试,它运行良好,但是,我无法在Laravel中获得正确的配置。

这是我的连接字符串>

$conn = new PDO("odbc:Driver={SQL Server};Server=MyHOST;Database=myDb;User Id=sa;Password=;");

到目前为止,我在Laravel config / database.php

中得到了这个

修改

好的,我按照https://github.com/ccovey/odbc-driver的说明进行了配置:

我把它改成了

'odbc' => array(
        'driver' => 'odbc',
        'dsn' => 'Driver={SQL Server};Server=MyServer',
        'grammar' => 'SqlServerGrammar',
        'username' => 'user',
        'password' => 'pass',
        'database' => 'staPPM',
    ),

我收到错误FatalErrorException,Grammar not Found

1 个答案:

答案 0 :(得分:3)

Laravel 4仍然不直接支持ODBC,您必须自己动手,或者您可以尝试使用此驱动程序:https://github.com/ccovey/odbc-driver

您必须添加看起来像这样的连接:

'odbc' => array(
   'driver' => 'odbc',
   'dsn' => 'Driver={iSeries Access ODBC Driver};System=my_system_name;',
   'grammar' => 'DB2',
   'username' => 'foo',
   'password' => 'bar',
   'database' => '',
   'grammar' => 'SqlServerGrammar',
),

如软件包文档中所述,您必须提供有效的DSN 才能连接到您的服务器:这些是有效连接字符串的示例:

"Driver={SQL Server};Server=(local);Trusted_Connection=Yes;Database=AdventureWorks;"

"Driver={Microsoft ODBC for Oracle};Server=ORACLE8i7;Persist Security Info=False;Trusted_Connection=Yes"

"Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\bin\Northwind.mdb"

"Driver={Microsoft Excel Driver (*.xls)};DBQ=c:\bin\book1.xls"

"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:\bin"

要知道您的DSN是否有效,您最好在Laravel之外测试DNS。

如果您有权访问Linux,可以通过执行以下操作进行测试:

apt-get install unixodbc

isql -v DSN_NAME db_username db_password

它应该回答:

+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL>

ccovey的源代码中存在一个错误,目前,您应该将ODBCDriverConnection的来源更改为:

/**
 * Default grammar for specified Schema
 * @return Schema\Grammars\Grammar
 */
protected function getDefaultSchemaGrammar()
{
    return $this->withTablePrefix(new \Illuminate\Database\Schema\SqlServerGrammar);
}

我将在Github包中打开一个问题,以便他们解决这个问题。