Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.