문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top