9

I am trying to connect to a Microsoft SQL Server database using the database/sql package for golang.

There is no MSSQL-specific driver listed at https://code.google.com/p/go-wiki/wiki/SQLDrivers, so I thought I'd try an odbc driver.

I tried https://github.com/weigj/go-odbc but when I run go install I receive cc1.exe: sorry, unimplemented: 64-bit mode not compiled in. This is listed as an open issue in the github repo.

Does anyone have experience connecting to an MSSQL database from a 64-bit Windows 7 client? Which odbc driver is recommended?

slachterman
  • 1,515
  • 4
  • 17
  • 23
  • 1
    https://code.google.com/p/odbc – alex Jun 02 '13 at 23:28
  • Alex, could you provide an example of a sql.Open() call using that driver? Is it necessary to use a DSN or can a connection string be specified? Thanks. – slachterman Jun 06 '13 at 00:03
  • 1
    https://code.google.com/p/odbc/source/browse/mssql_test.go#56 – alex Jun 06 '13 at 00:20
  • anyone using `github.com/denisenkom/go-mssqldb` driver for SQL Server may be helped by my answer to [Go with SQL Server driver is unable to connect successfully, login fail](http://stackoverflow.com/questions/32010749/go-with-sql-server-driver-is-unable-to-connect-successfully-login-fail) which details some of what I went through to make a demo program work. There are several SQL Server configuration changes I had to make. – Richard Chambers Aug 18 '16 at 13:15

2 Answers2

10

Now, there is a Microsoft SQL Server specific driver on the database driver list SQL database drivers in github with a pure Go package https://github.com/denisenkom/go-mssqldb

You could try go-mssqldb to connect mssql directly.

The import could look like:

import (
    "fmt"
    "log"
    "database/sql"
     _ "github.com/denisenkom/go-mssqldb"     // the underscore indicates the package is used
)    

the sql.Open() looks like:

// the user needs to be setup in SQL Server as an SQL Server user.
// see create login and the create user SQL commands as well as the
// SQL Server Management Studio documentation to turn on Hybrid Authentication
// which allows both Windows Authentication and SQL Server Authentication.
// also need to grant to the user the proper access permissions.
// also need to enable TCP protocol in SQL Server Configuration Manager.
condb, errdb := sql.Open("mssql", "server=localhost;user id=gouser;password=g0us3r;")
if errdb  != nil {
    fmt.Println("  Error open db:", errdb.Error())
}

defer condb.Close()

and I am using it, it's ok for now.

Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
Sheppard Y
  • 101
  • 1
  • 5
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – juliocesar Apr 25 '14 at 04:08
7

Try using this ODBC driver instead, I believe it is more widely used: https://code.google.com/p/odbc/

voidlogic
  • 6,398
  • 2
  • 23
  • 21
  • Also, depending on your platform, may need to use `FreeTDS` over the `Microsoft ODBC SQL Adapter` for ODBC clients (specifically non-windows). – Tracker1 Mar 06 '15 at 17:31