将SQL-Server直接连接到联机数据库rails activerecord-sqlserver-adapter

时间:2016-04-13 08:33:47

标签: ruby-on-rails sql-server database-connection

我刚安装了[README.md][1]文件中提到的两个宝石。我有一个查询字符串,我想直接从localhost连接到数据库..有可能吗?

class Timelapse < ActiveRecord::Base
  establish_connection({
    :adapter  => "sqlserver",
    :host     => "myhostname.net",
    :username => "myusername",
    :password => "mypassword",
    :database => "mydbname"
  })
end

我去rails c试图做Timelapse.first并收到错误

TinyTds::Error: Adaptive Server connection failed
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/tiny_tds-0.7.0/lib/tiny_tds/client.rb:74:in `connect'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/tiny_tds-0.7.0/lib/tiny_tds/client.rb:74:in `initialize'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-sqlserver-adapter-4.2.10/lib/active_record/connection_adapters/sqlserver_adapter.rb:311:in `new'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-sqlserver-adapter-4.2.10/lib/active_record/connection_adapters/sqlserver_adapter.rb:311:in `dblib_connect'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-sqlserver-adapter-4.2.10/lib/active_record/connection_adapters/sqlserver_adapter.rb:302:in `connect'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-sqlserver-adapter-4.2.10/lib/active_record/connection_adapters/sqlserver_adapter.rb:58:in `initialize'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-sqlserver-adapter-4.2.10/lib/active_record/sqlserver_base.rb:17:in `new'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-sqlserver-adapter-4.2.10/lib/active_record/sqlserver_base.rb:17:in `sqlserver_connection'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
    from /home/ijunaidfarooq/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
    from /home/ijunaidfarooq/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:87:in `connection'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.6/lib/active_record/querying.rb:39:in `find_by_sql'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.6/lib/active_record/relation.rb:639:in `exec_queries'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.6/lib/active_record/relation.rb:515:in `load'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.6/lib/active_record/relation.rb:243:in `to_a'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.6/lib/active_record/relation.rb:630:in `inspect'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/railties-4.2.6/lib/rails/commands/console.rb:110:in `start'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/ijunaidfarooq/.rvm/gems/ruby-2.2.2/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:8:in `require'

1 个答案:

答案 0 :(得分:0)

使用这些Gem

这样连接

setp: - 1 gem&#39; tiny_tds&#39; gem&#39; activerecord-sqlserver-adapter&#39;

并使用git activerecord-sqlserver-adapter

进行检查

步骤:-2 发展:   港口:1433   模式:dblib   适配器:sqlserver   host:当dataserver为空时使用#   数据库:   用户名:   密码:   超时:0

默认   端口:1433#默认为1433   默认   模式:dblib   数据服务器:   超时:5000   超时:0 - 没有超时   天蓝色:真实的   dsn: