我正在尝试查询SQL Server 2008 R2
使用去
https://github.com/denisenkom/go-mssqldb https://github.com/denisenkom/go-mssqldb.
SQL Server 2008 R2实例位于Windows Server 2008 R2下的VM上;我正在 Win 7 VMWare 主机下进行开发,并从那里运行程序来查询虚拟机上的数据库。数据库已启动并正在运行服务器虚拟机上托管的应用程序。代码如下。
我收到的错误是:
[编辑 2017-03-14:指定端口时出现新错误]
登录错误:读取tcp 192.168.91.1:15222->192.168.91.135:1433:
wsarecv:现有连接被远程主机强制关闭。
指定 SQL Server 端口 (1433) 时会返回此错误。包含或不包含实例不会改变它。
SQL Server 配置为允许远程连接、SQL Server 身份验证、连接未加密、启用 TCP/IP、IPALL 端口=1433。防火墙对 80、443、1433、1434 上的 TCP 开放; 1433、1434 上的 UDP。在将数据库实例添加到连接字符串中之前,我收到了不同的错误。
SQL 服务器日志seem表明机器正在说话。 IP 地址用于 VMWare 主机和 VM。SQL Server 浏览器服务正在运行(帐户“本地服务”)。 SQL Server 代理是not跑步。我尝试过使用 ODBC 和 ADO 连接字符串,但似乎遇到了相同的错误。任何帮助,将不胜感激。
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