我用mysql_connect()连接MySql数据库正常,但是用mssql_connect()连接不上,提示不能连接到数据库。用PDO连接同样连接不上。之前我用C++做的一个程序连接我的SQL SERVER一切正常,请问是什么原因呢,求解释啊,急急急我把我的PDO连接和错误代码都挂上,方便高手解答,万分感谢
$dbh = new PDO('mssql:host=localhost;dbname=MyData','sa','*****');//密码已隐藏
就这一句代码,连接不上,错误提示是:
'SQLSTATE[01002] Unable to connect: SQL Server is unavailable or does not exist. Access denied. (severity 9)'

解决方案 »

  1.   


    Example #1 PDO_SQLSRV DSN examples
    The following example shows how to connecto to a specified MS SQL Server database:
    $c = new PDO("sqlsrv:Server=localhost;Database=testdb", "UserName", "Password");
    The following example shows how to connect to a MS SQL Server database on a specified port:
    $c = new PDO("sqlsrv:Server=localhost,1521;Database=testdb", "UserName", "Password");
    The following example shows how to connecto to a SQL Azure database with server ID 12345abcde. Note that when you connect to SQL Azure with PDO, your username will be UserName@12345abcde (UserName@ServerId).
    $c = new PDO("sqlsrv:Server=12345abcde.database.windows.net;Database=testdb", "UserName@12345abcde", "Password");
    PDO_SQLSRV DSN(No version information available, might only be in SVN)PDO_SQLSRV DSN — Connecting to MS SQL Server and SQL Azure databases说明The PDO_SQLSRV Data Source Name (DSN) is composed of the following elements:DSN prefix
    The DSN prefix is sqlsrv:.APP
    The application name used in tracing.ConnectionPooling
    Specifies whether the connection is assigned from a connection pool (1 or TRUE) or not (0 or FALSE).Database
    The name of the database.Encrypt
    Specifies whether the communication with SQL Server is encrypted (1 or TRUE) or unencrypted (0 or FALSE).Failover_Partner
    Specifies the server and instance of the database's mirror (if enabled and configured) to use when the primary server is unavailable.LoginTimeout
    Specifies the number of seconds to wait before failing the connection attempt.MultipleActiveResultSets
    Disables or explicitly enables support for multiple active Result sets (MARS).QuotedId
    Specifies whether to use SQL-92 rules for quoted identifiers (1 or TRUE) or to use legacy Transact-SQL rules (0 or false).Server
    The name of the database server.TraceFile
    Specifies the path for the file used for trace data.TraceOn
    Specifies whether ODBC tracing is enabled (1 or TRUE) or disabled (0 or FALSE) for the connection being established.TransactionIsolation
    Specifies the transaction isolation level. The accepted values for this option are PDO::SQLSRV_TXN_READ_UNCOMMITTED, PDO::SQLSRV_TXN_READ_COMMITTED, PDO::SQLSRV_TXN_REPEATABLE_READ, PDO::SQLSRV_TXN_SNAPSHOT, and PDO::SQLSRV_TXN_SERIALIZABLE.TrustServerCertificate
    Specifies whether the client should trust (1 or TRUE) or reject (0 or FALSE) a self-signed server certificate.WSID
    Specifies the name of the computer for tracing.
      

  2.   

    你通过sql server相关工具能连上sql server吗   加上端口试试。
      

  3.   

    加上端口号也是不成功,怎么用sql server相关工具能连上sql server啊,能具体说一下吗,我怀疑是我的SQLSERVER的问题,可是我用C++写的程序能连接上啊,刚刚测试了
      

  4.   

    这就不懂了。你要去操作sql server自己还不懂么? 用QL Server Configuration Manager 连连试试。
      

  5.   

    SQL Server Configuration Manager 
      

  6.   

    恩,谢谢,用那个连接是成功的
    我找到问题了,使用mssql_connect的时候,那个servername,不可以用localhost,127.0.0.1,用这个都显示找不到服务器,换成计算机名称就好了,晕,耽误我半天了
      

  7.   


    呵呵,几百年没碰过mssql 我记得我那时候碰也遇到过你的问题,你不说我忘记了.不过我记得是可以在sql里设置允许的.