Skip to content

Instantly share code, notes, and snippets.

@burningTyger
Forked from thinkAmi/sequel_to_ms_access.rb
Created December 12, 2016 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burningTyger/3c021e0f212e8f85e7d73bea55341011 to your computer and use it in GitHub Desktop.
Save burningTyger/3c021e0f212e8f85e7d73bea55341011 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