Skip to content

Instantly share code, notes, and snippets.

@thinkAmi
Created June 6, 2014 21:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save thinkAmi/e928902f7d0e2205a35b to your computer and use it in GitHub Desktop.
Save thinkAmi/e928902f7d0e2205a35b to your computer and use it in GitHub Desktop.
Sequel: Connecting to Microsoft Access(*.accdb)
# Tested Environment
# Windows7 x64, MS Access2010/2013, sequel 4.11.0
# Gemfile
# source 'https://rubygems.org'
# gem 'sequel'
# gem 'ruby-odbc' # for ODBC
require 'sequel'
ACCDB_PATH = '//127.0.0.1/db/Sample.accdb'
OLEDB_PROVIDER = 'Microsoft.ACE.OLEDB.12.0'
ODBC_DRIVER = '{Microsoft Access Driver (*.mdb, *.accdb)}'
DSN = 'SequelTest'
def test(db, name)
db.test_connection
p "#{name}: OK"
rescue Sequel::DatabaseConnectionError
p "#{name}: ERROR"
end
# Using ADO (Unuse gem 'ruby-odbc')
db1 = Sequel.ado(conn_string: "Provider=#{OLEDB_PROVIDER};Data Source=#{ACCDB_PATH}")
test(db1, 'ADO')
# Using ODBC(Need gem 'ruby-odbc')
# DSN
db2 = Sequel.odbc(DSN)
test(db2, 'DSN')
# DSN Less
db3 = Sequel.odbc(drvconnect: "Driver=#{ODBC_DRIVER};Dbq=#{ACCDB_PATH};")
test(db3, 'DSN Less')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment