HDBC-ODBC MS SQL under Linux

From HaskellWiki
Jump to navigation Jump to search

A brief description of how to get HDBC-ODBC working under linux, for connecting to MS SQL server.

Install

  • Ensure your path contains the ghc bin directory:
  • Ensure you have unixODBC and freetds package installed
  • Download and unpack the most recent versions of hdbc and hdbc-odbc hackage.haskell.org
  • Follow the README instructions for installing hdbc
  • Follow the README instructions for installing hdbc-odbc

odbcinst.ini

declare a SQL Server driver, correcting the location of the .so files according to your system:

[SQL Server]
Description = TDS driver MSSQL
Driver = /usr/lib/libtdsodbc.so
Setup = /usr/lib/libtdsS.so

Test

You can test it from a ghci prompt (obviously you'll need to change the connection string and query):

Prelude> :m Database.HDBC
Prelude Database.HDBC> :m + Database.HDBC.ODBC
Prelude Database.HDBC Database.HDBC.ODBC> let connectionString =  "Driver={SQL Server};Port=1433;Server=myserver;Database=mydatabase;Trusted_Connection=yes;"
Prelude Database.HDBC Database.HDBC.ODBC> let ioconn = connectODBC connectionString
Prelude Database.HDBC Database.HDBC.ODBC> do { conn <- ioconn; vals <- quickQuery conn "SELECT getdate();" []; print vals }
[[SqlLocalTime ...

If you are not using a DSN, it's critical to explicitly set the Port in the connection string