Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2008
    Posts
    6

    Unanswered: Connect to SqlServer 2005 with odbc

    Hi all,
    I'm not a db guru and probably this is a very simple question for you, but I was wondering if it was possible to connect to a SqlServer using odbc without DSN.
    I mean, I'm trying to connect to the db server from a linux (Fedora Core 8) workstation, using odbc drivers, but it looks like I can do that only using a DSN connection and setting properly the odbc.ini file. It doesn't work, if I use a connection like this from a PHP script:
    Code:
    $cn = odbc_connect("Driver={SQL Server};Server=MyServer;Database=MyDatabase",
    "MyUser","MyPassword")
    Does anyone know how to connect to SqlServer with a DSN-less connection? Could you explain me where is the error?

    I'm using SQLServer2005, Fedora Core 8 with the last FreeTDS installed.

    Please help!

    Thanks million in advance.

  2. #2
    Join Date
    Jun 2007
    Location
    Ohio, USA
    Posts
    142
    Not my forte, but I'd start here: http://connectionstrings.com/?carrier=sqlserver2005.

    What error do you get?

    -D.

  3. #3
    Join Date
    Feb 2008
    Posts
    6
    Thank you Ready, but I've already tried without success the connection string that you linked in your message!
    When I tried a DSN-less connection I got the following error message:
    Code:
    [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified, SQL state IM002 in SQLConnect
    When I use DSN everything works fine!

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    You're not assigning the username and passwordy bits right.
    Code:
    ...Uid=myUsername;Pwd=myPassword;
    George
    Home | Blog

  5. #5
    Join Date
    Feb 2008
    Posts
    6
    You're right Georgev, but mine was only an example. I've tried also with your code and it gives always the same error.

  6. #6
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Well, let it be noted that instead of posting an example which we spend time solving for no reason, you instead post all the actual code and we only have to do this once
    George
    Home | Blog

  7. #7
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    do we have the client tools and the ODBC drivers installed on the client which I assume is a web server here?
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  8. #8
    Join Date
    Feb 2008
    Posts
    6
    Quote Originally Posted by georgev
    Well, let it be noted that instead of posting an example which we spend time solving for no reason, you instead post all the actual code and we only have to do this once
    Ok georgev, you're right, my fault. The fact is that I've tried a lot of code without success. Anyway this is the code with DSN-less connection that doesn't work and that gives me the aforementioned error:
    Code:
    $connection_string = 'Driver={SQL Native Client};
                                  Server=mydbaddress;
                                  Database=mydbname;
                                  Uid=mydbuser;
                                  Pwd=mydbpass;';
    
    $connection = odbc_connect($connection_string,'','');
    
    $rst = odbc_exec($connection,$sql_statement);
    
    if (!$rst){
        print "<p>- SQL Server Query failed ODBC: " . odbc_errormsg($connection) . "</p>";
    }
    This one, instead, is the connection that uses the DSN specified in my odbc.ini and that works well:
    Code:
    $dsn = "DSNodbc";
    
    $connection = odbc_connect($dsn, 'mydbuser', 'mydbpass');
    
    $rst = odbc_exec($connection,$sql_statement);
    
    if (!$rst){
        print "<p>- SQL Server Query failed con ODBC: ". odbc_errormsg($connection)."</p>";
    }
    Just don't care about the sql statment, it's a simple test query.
    You're right Thrasymachus, it's an Apache platform and everything looks installed well (otherwise I think that also the DSN connection should not work ).

    Thaks a lot for your time

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •