通过go-mssqldb在golang中查询SQL Server

huangapple go评论90阅读模式
英文:

golang SQL Server query via go-mssqldb

问题

我正在尝试使用go查询SQL Server 2008 R2。你可以使用go-mssqldb库来实现。SQL Server 2008 R2实例运行在Windows Server 2008 R2的虚拟机上,我在Win 7的VMWare主机下进行开发,并从那里运行程序来查询虚拟机上的数据库。下面是代码:

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()
}

你遇到的错误是:

[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.

当指定了SQL Server的端口(1433)时,会出现这个错误。无论是否包含实例,都不会改变这个错误。

SQL Server已配置为允许远程连接,使用SQL Server身份验证,连接未加密,启用了TCP/IP,IPALL端口设置为1433。防火墙已打开TCP的80、443、1433、1434端口,UDP的1433、1434端口。在将数据库实例添加到连接字符串之前,我遇到了不同的错误。

SQL Server日志似乎表明机器之间正在通信。IP地址是VMWare主机和虚拟机的地址。SQL Server Browser服务正在运行(账户为"Local Service")。SQL Server Agent 未运行。我尝试使用ODBC和ADO连接字符串,似乎都遇到了同样的错误。希望能得到帮助。

输出结果为:

[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

希望对你有所帮助。

英文:

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

答案1

得分: 1

我在Github上作者的评论中找到了答案。

在连接字符串中添加"encrypt=disable"就可以了。我正在下载SQL Server 2008 R2 x64的SP3更新,这里建议的方式,等我有时间了再安装它。不过目前查询已经可以工作了。

英文:

I found the answer in a comment by the library author on Github.

Adding the "encrypt=disable" to the connection string did it. I'm downloading the SP3 update for SQL Server 2008 R2 x64 as suggested here and will install it when I get some time. As for now though the query works.

答案2

得分: 0

GitHub存储库中提到:

确保SQL Server Browser Windows服务正在运行,并且没有阻止UDP端口1434的防火墙。驱动程序使用此服务获取SQL Server实例的TCP端口。

确保您的主机上SQL Browser服务正在运行。

此外,您可以在连接字符串中指定端口号(如果您的端口是静态的,则可以避免启动SQL Browser服务的需要)。

英文:

The github repo says below

>Ensure that the SQL Server Browser windows service is running and there is no firewall blocking UDP port 1434. This service is used by the driver to get the TCP port of the SQL Server instance

Ensure your SQLbrowser service is running on your host..

also you can specify your connection string along with port number(this negates the need of starting SQLBrowser service,if your port is static)

huangapple
  • 本文由 发表于 2017年3月13日 14:06:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/42757477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定