Unable to connect to MSSQL 2014 with Golang

I’m trying to connect my Golang app to MSSQL 2014.

I have been successful using - https://github.com/denisenkom/go-mssqldb For this, I had to enable a static TCP/IP port on my MSSQL & things were good, but it didn’t work with the default enabled TCP/IP port - 1433.

But what I want to achieve is to connect to MSSQL with the default open TCP/IP port on MSSQL, as my PHP app already does this using PDO.

I also tried using - https://github.com/minus5/gofreetds but was unable to install FreeTDS on Windows.

Another golang pkg that I came across was - https://github.com/alexbrainman/odbc But due to lack of doc, I’m still stuck. Unit tests for this pkg fail, saying the user login failed. But the login creds are working fine.

Has anyone been successful in achieving this.

What’s the error message? It’s hard to say without that.

I’ve been using github.com/denisenkom/go-mssqldb and it ever worked just fine for me. I don’t recommend the other two drivers.

Hi Andrey,
Thanks for your reply.
I used github.com/denisenkom/go-mssqldb, but it only works if I explicitly enable 1433 Port for SQL Server.
Whereas when I use github.com/alexbrainman/odbc, I don’t need to enable any ports.
Everything works with fine with github.com/alexbrainman/odbc with SQL Server 2014.
This is my connection string -
Driver={SQL Server Native Client 11.0};Server=" + ptrServer + “;Database=” + ptrDBName + “;Uid=” + ptrUser + “;Pwd=” + ptrPass
But it fails sometimes with SQL Server 2012 & SQL Server 2008.
The error that I get is - “unable to open database file”

You are saying that you don’t recommend other drivers, is there a reason for that?
If possible, can you tell me how can I achieve connecting to SQL Server using github.com/denisenkom/go-mssqldb, without explicitly opening a port.
Thanks again for your help, really appreciate :slight_smile:

unable to open database file

This error seems very strange to me, and I think it suggest there’s some misconfiguration of the server.

I think informing the port is usually not necessary, but depends on the server, too. Are you connection with SERVER\INSTANCE format or you are informing just SERVER?

I recommend https://github.com/denisenkom/go-mssqldb because (as it seems) it is the only dependency-free (pure Go implementation). Also very stable in my experience.

I’m using SERVER\INSTANCE for the connection.
I know the server is configured properly because my PHP app connects fine using PDO Objects (php_pdo_sqlsrv_7_nts_x64.dll)
So I was thinking if everything works fine using PHP, then it should be the same for Go!
Also, I didn’t need to install any dependencies for github.com/alexbrainman/odbc.
Basically trying to achieve the same thing in Go, as already done with PHP :slight_smile:

I wasn’t entering any port in the connection string as well for github.com/denisenkom/go-mssqldb but It wouldn’t connect for me until I went into SQL Server and enabled 1433 port (which I don’t want to do).

Any suggestions would be helpful!

You seem to be doing everything right…

Maybe you can check if connection via TCP/IP is enabled on the server. Also, you can check if the “SQL Browser” service is enabled and running.

denisenkom’s driver works very well. The port must be open in firewall on the server side and probably is opened, I don’t think you must do something in this way. Also try using ;encrypt=disable as option in connection string.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.