Sql-server – Golang MSSQL driver for Windows7 64-bit

64-bitgosql-serverwindows-7-x64

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?

Best Solution

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.

Related Question