Search code examples
rubysql-serversspitiny-tds

Using SSPI with Ruby TinyTDS - possible?


After some anguish trying to connect to a SQLServer database with Ruby, I finally discovered TinyTDS and it's fantastic.

However, it requires a username and password to talk to the database. In C# tests in the past, we've used SSPI to supply this, so that any tester can pick up a script and run it and it'll use their Windows Authentication details.

I can't find a way to do this with TDS (beginning to suspect it's not possible with the current version) and hoping someone might prove me wrong, or have another suggestion?

Cheers.


Solution

  • Found the solution.

    My install of tiny-tds was version 0.51.

    The latest version has SSPI, and so to get that:

    gem install tiny_tds --version ">= 0.6.0.rc1"
    

    This comes with no need to specify a username/password and use SSPI by default.

    So as an example:

    require  'tiny_tds'
    
        sql = "SELECT name from sys.databases"
        client = TinyTds::Client.new(:dataserver => "myserver", :database => "mydatabase")
        result = client.execute(sql)
        results = result.each(:symbolize_keys => true, :as => :array, :cache_rows => true, :empty_sets => true) do |rowset| end
        #THIS IS TO OUTPUT IT TO THE CONSOLE
        for i in (0..result.fields.length)
          printf("%14s", result.fields[i])
        end
        for j in (0...result.affected_rows)
          puts ""
          for i in (0...result.fields.length)
            printf("%14s",results[j].at(i))
          end
        end
    

    Will print out a list of the database names, using SSPI to access the database.