I'm trying to query SQL Server 2008 R2 using go
https://github.com/denisenkom/go-mssqldb.
The SQL Server 2008 R2 instance is on a VM under Windows Server 2008 R2; I'm doing the development under the Win 7 VMWare host and running the program from there to query the DB on the VM. The DB has been up and running an app hosted on the server VM. Code is below.
The error I'm getting is :
[EDIT 2017-03-14 : new error when I specify port]
Login error: read tcp 192.168.91.1:15222->192.168.91.135:1433: wsarecv: An existing connection was forcibly closed by the remote host.
This error is returned when the SQL Server port (1433) is specified. Including or not including the instance doesn't change it.
SQL Server is configured to allow remote connections, SQL Server Auth, Connection not encrypted, TCP/IP enabled, IPALL port=1433. Firewall is open for TCP on 80, 443, 1433, 1434; UDP on 1433, 1434. I was getting a different error until I added the db instance into the connection string.
The SQL server logs seem to indicate that the machines are talking. The IP addresses are for the VMWare host and the VM. The SQL Server Browser service is running (acct "Local Service"). SQL Server Agent is not running. I've tried using ODBC and ADO connection strings and seem to get the same error. Any help would be appreciated.
package main
import (
// Import go-mssqldb strictly for side-effects
_ "github.com/denisenkom/go-mssqldb"
"database/sql"
"log"
)
func main() {
var n_tables int
println (sql.Drivers())
// URL connection string formats
// sqlserver://sa:mypass@localhost?database=master&connection+timeout=30 // username=sa, password=mypass.
// sqlserver://sa:my%7Bpass@somehost?connection+timeout=30 // password is "my{pass"
// note: pwd is "myP@55w0rd"
connectString := "sqlserver://SBM:myP%4055w0rd@VM17:1433?database=AE&connection+timeout=30"
println("Connection string=" , connectString )
println("open connection")
db, err := sql.Open("mssql", connectString)
defer db.Close()
println ("Open Error:" , err)
if err != nil {
log.Fatal(err)
}
println("count records in TS_TABLES & scan")
err = db.QueryRow("Select count(*) from ts_tables").Scan(&n_tables)
if err != nil {
log.Fatal(err)
}
println ("count of tables" , n_tables)
println("closing connection")
db.Close()
}
output:
[2/2]0xc042002c20
Connection string= sqlserver://VM17_SBM:P%4055word@VM17:1433?database=VM17_SBM_AE_OE_REPO_CL&connection+timeout=30
open connection
Open Error: (0x0,0x0)
count records in TS_TABLES & scan
2017/03/14 19:48:01 Login error: read tcp 192.168.91.1:15222->192.168.91.135:1433: wsarecv: An existing connection was forcibly closed by the remote host.
exit status 1